search

Home  >  Q&A  >  body text

LARAVEL8: Trying to access 'id' property on Null

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:

<section class="col-span-8 col-start-5 mt-10 space-y-6">
    <!-- Post form -->
    <form method="POST" action="/post/{{ $post->slug }}/comments" class="border border-gray-200 p-6 rounded-xl">
        @csrf
        <header class="flex items-center">
            <img src="https://i.pravatar.cc/100?id={{ auth()->id() }}" alt="" width="40" height="40" class="rounded-full">
            <h2 class="ml-3 ">Want to participate?</h2>
        </header>
        <div class="mt-6">
            <textarea class="w-full text-sm focus:outline-none focus:ring"
             name="body"  
             cols="30" rows="10"
              placeholder="Quick,think of something to say!" ></textarea>
        </div>
    
        <div>
            <button type="submit" class="bg-blue-500 text-white uppercase font-semi-bold text-xs py-2 px-10 rounded-2xl hover:bg-blue-600">Post</button>
        </div>

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 example auth()-> id, Auth::user()->id

<?php

namespace AppHttpControllers;

use AppModelsPost;


class PostCommentsController extends Controller
{
    public function store(Post $post){

        
        request()->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

P粉794851975P粉794851975404 days ago683

reply all(1)I'll reply

  • P粉952365143

    P粉9523651432023-11-06 00:33:48

    Use this code for the controller

    class PostCommentsController extends Controller
    {
    public function store(Post $post){
    
        
        request()->validate([
            'body'=>'required'
        ]);
    
        $post->comments()->create([
            'user_id'=> optional(auth()->user())->id,
            'body' => request('body')
        ]);
    
        return back();
    }
    }

    User must log in

    reply
    0
  • Cancelreply