1 year ago

Can't post comments after completing Laravel 5.4 episode 19

Posted 1 year ago by kelvinangular

I get the following error when trying to post a comment:

I. Error

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into comments (body, post_id, updated_at, created_at) values (This, 1, 2018-01-17 03:42:50, 2018-01-17 03:42:50))

II. CommentsController

use App\Post; use App\Comment; class CommentsController extends Controller { public function store(Post $post) { $this->validate(request(), ['body' => 'required|min:2']); $post->addComment(request('body')); return back(); } }

III. Comment Model

namespace App; class Comment extends Model { public function post() { return $this->belongsTo(Post::class); } public function user() { return $this->belongsTo(User::class); } }

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