I'm new to Laravel and I'm learning it from Laracast. Here is my problem, I am creating a comment form and the php code for it looks like this:
This is the corresponding route:
Route::post('post/{post:slug}/comments',[PostCommentsController::class, 'store']);
Controller:, I suspect there may be something wrong here'user_id'=> request()->user()->id
, I tried multiple ways to implement this approach , for exampleauth()-> id, Auth::user()->id
validate([ 'body'=>'required' ]); $post->comments()->create([ 'user_id'=> request()->user()->id, 'body' => request('body') ]); return back(); } }
This is the migration table of comments
Schema::create('comments', function (Blueprint $table) { $table->id(); $table->foreignId('post_id')->constrained()->cascadeOnDelete(); $table->foreignId('user_id')->constrained()->cascadeOnDelete(); $table->text('body'); $table->timestamps();
Post migration table:
Schema::create('posts', function (Blueprint $table) { $table->id(); $table->foreignId('user_id')->constrained()->cascadeOnDelete(); $table->foreignId('category_id'); $table->string('slug')->unique(); $table->string('title'); $table->text('excerpt'); $table->text('body'); $table->timestamps(); $table->timestamp('published_at')->nullable(); });
If I click the publish button I get the above error, I have tried my best to fix this but I can't resolve it. Can anyone help me what's wrong with my code? My question may seem naive as I am new to the stackoverflow community
Use this code for the controller
User must log in