Answer the question
In order to leave comments, you need to log in
Why doesn't the commentary work?
Hello, when I try to leave a comment on the site, I get this error
Illuminate\Database\QueryException (HY000)
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `comments` (`name`, `text`, `post_id`, ` updated_at`, `created_at`) values (Test, Hello 1, 2019-04-22 12:08:26, 2019-04-22 12:08:26))
Previous exceptions
SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (HY000)
"SQLSTATE[HY000]: General error: 1364 Field 'id' doesn't have a default value (SQL: insert into `comments` (`name`, `text` , `post_id`, `updated_at`, `created_at`) values (Test, Hello 1, 2019-04-22 12:08:26, 2019-04-22 12:08:26))
namespace App;
use Illuminate\Database\Eloquent\Model;
class Comment extends Model
{
protected $fillable = ['id', 'name', 'text', 'post_id', 'status'];
public function post()
{
return $this->belongsTo('App\Post');
}
}
public function comment(Request $request)
{
$this->validate($request,[
'name' => 'required',
'text' => 'required',
]);
Comment::create([
'name' => $request->name,
'text' => $request->text,
'post_id' => $request->post_id,
]);
return redirect()->back()->with('status', 'Комментарий добавлен');
}
Answer the question
In order to leave comments, you need to log in
You have a crooked table schema - the id field must be marked as auto-incrementing.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question