mirror of
https://github.com/10h30/Test-Laravel-Eloquent-Basics.git
synced 2026-06-05 15:07:45 +09:00
Task 5 - simple create
This commit is contained in:
@@ -59,3 +59,12 @@ In `app/Http/Controllers/UserController.php` file method `check_create()`, find
|
||||
|
||||
Test method `test_check_or_create_user()`.
|
||||
|
||||
---
|
||||
|
||||
## Task 5. Create a New Record.
|
||||
|
||||
In `app/Http/Controllers/ProjectController.php` file method `store()`, creating the project will fail. Fix the underlying issue, to make it work.
|
||||
|
||||
Test method `test_create_project()`.
|
||||
|
||||
---
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Project;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class ProjectController extends Controller
|
||||
{
|
||||
public function store(Request $request)
|
||||
{
|
||||
// TASK: Currently this statement fails. Fix the underlying issue.
|
||||
Project::create([
|
||||
'name' => $request->name
|
||||
]);
|
||||
|
||||
return redirect('/')->with('success', 'Project created');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Project extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
class CreateProjectsTable extends Migration
|
||||
{
|
||||
/**
|
||||
* Run the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function up()
|
||||
{
|
||||
Schema::create('projects', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('name');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function down()
|
||||
{
|
||||
Schema::dropIfExists('projects');
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
use Illuminate\Support\Facades\Route;
|
||||
use App\Http\Controllers\UserController;
|
||||
use App\Http\Controllers\ProjectController;
|
||||
|
||||
/*
|
||||
|--------------------------------------------------------------------------
|
||||
@@ -21,3 +22,5 @@ Route::get('/', function () {
|
||||
Route::get('users', [UserController::class, 'index']);
|
||||
Route::get('users/{userId}', [UserController::class, 'show']);
|
||||
Route::get('users/check/{name}/{email}', [UserController::class, 'check_create']);
|
||||
|
||||
Route::post('projects', [ProjectController::class, 'store']);
|
||||
|
||||
@@ -69,4 +69,9 @@ class EloquentTest extends TestCase
|
||||
$this->assertDatabaseCount('users', 1);
|
||||
}
|
||||
|
||||
public function test_create_project() {
|
||||
$response = $this->post('projects', ['name' => 'Some name']);
|
||||
$response->assertRedirect();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user