artisticre
1 month ago
345
4
Laravel

Call to a member function isValid() on null

Posted 1 month ago by artisticre

I know its on the validation because if I take the validation out, it posts a comment. Not sure what to fix.

create function

public function store(Request $request,$post_id)
    {
        

        $this->validate($request, array(
            'name' => 'required|max:255',
            'email' => 'required|email:max:255',
            'comment' => 'required|min:5|max:2000'
           
        ));

        $post = Post::find($post_id);
        $comment = new Comment();
        $comment->name = $request->name;
        $comment->email = $request->email;
        $comment->comment = $request->comment;
        $comment->approved = true;
        $comment->post()->associate($post);
        $comment->save();
        Session::flash('success','Comment was added!');
        return redirect()->route('blog.single',[$post->slug]);
    }

migration

public function up()
    {
        Schema::create('comments', function (Blueprint $table) {
            $table->id();
            $table->string('name');
            $table->string('email');
            $table->text('comment');
            $table->boolean('approved');
            $table->integer('post_id')->unsigned();
            $table->timestamps();
        });

        Schema::table('comments', function($table) {
        $table->foreign('post_id')->references('id')->on('posts')->onDelete('cascade');

    });

Please sign in or create an account to participate in this conversation.