This may be a very simple question, but I can't figure it out! and that's frustrating. I do my best to explain everything step by step.
- This a small Todo list project in Laravel 8
- A user can create a Project.
- When user clicks on a created project, he goes to the project page (show page)
ShowController.php
public function show(Project $project)
{
return view('projects.show', compact('project'));
}
- In the show page the user can add comments via a textarea form field.
show.blade.php
<form action="{{ route('project.update',['project' => $project]) }}" method="post">
@csrf
@method('PUT')
<textarea name="notes" placeholder="Add notes">{{ $project->notes ?? '' }}</textarea>
<button type="submit">Save</button>
</form>
- Where it goes wrong is here, by updating the project! as soon as the user enters something in the comments field and clicks save, the form indicates that the following items are required:
- The owner_id, title, description field are required. While the model is sent to the show blade page and then in the form action route. What am I doing wrong here?
UpdateController.php
public function update(ProjectRequest $request, Project $project)
{
$validated = $request->validated();
$project->update($validated);
return redirect($project->path());
}
ProjectRequest.php
public function rules(): array
{
return [
'owner_id' => 'required',
'title' => 'required',
'description' => 'required',
'notes' => 'nullable',
];
web.php
use App\Http\Controllers\Projects\CreateController;
use App\Http\Controllers\Projects\IndexController;
use App\Http\Controllers\Projects\ShowController;
use App\Http\Controllers\Projects\StoreController;
use App\Http\Controllers\Projects\UpdateController;
use Illuminate\Support\Facades\Route;
Route::get('/', [IndexController::class, 'index'])->name('project.index');
Route::get('/projects/create', [CreateController::class, 'create'])->name('project.create');
Route::post('/projects', [StoreController::class, 'store'])->name('project.store');
Route::get('/projects/{project}', [ShowController::class, 'show'])->name('project.show');
Route::put('/projects/{project}', [UpdateController::class, 'update'])->name('project.update');
migration
public function up()
{
Schema::create('projects', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('owner_id');
$table->string('title');
$table->text('description');
$table->text('notes')->nullable();
$table->timestamps();
$table->foreign('owner_id')
->references('id')
->on('users')
->onDelete('cascade');
});
}