I'm trying to insert data into a table using Laravel Tinker and I'm getting the error:
Illuminate\Database\QueryException SQLSTATE[HY000]: General error: 1364 Field 'category_id' doesn't have a default value (Connection: mysql, SQL: insert into
posts
(title
,excerpt
,body
,updated_at
,created_at
) values (My Family Post, Excerpt for my post, Lorem Ipsum dolor sit amet., 2023-10-18 16:21:36, 2023-10-18 16:21:36)).
This is what I tried:
use App\Models\Post;
> Post::create([ 'title' => 'My Family Post', 'excerpt' => 'Excerpt for my post', 'body' => 'Lorem Ipsum dolor sit amet.', 'slug' => 'my-family-post', 'category_id' => 1]);
This error means, that you have one more field category_id
in your table posts
, but you did not specify value for this field.
Also, MySql know nothing about situation, that it should put in this field, if this field is not presented in the insert
statemen.
So, you have 2 options:
add field category_id
into your statement, thomething like this:
Post::create([ 'title' => 'My Family Post', 'excerpt' => 'Excerpt for my post', 'body' => 'Lorem Ipsum dolor sit amet.', 'slug' => 'my-family-post', 'category_id' => 1], category_id => null);
modify structure of table post
and indicate, which value MySQL should put, if this value is not presented.