M
M
matros972019-04-22 15:53:01
Laravel
matros97, 2019-04-22 15:53:01

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

Here is the comment model
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');
    }
}

Here is the controller code
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

1 answer(s)
A
Alexey Ukolov, 2019-04-22
@matros97

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 question

Ask a Question

731 491 924 answers to any question