[php] General error: 1364 Field 'user_id' doesn't have a default value

I am trying to assign the user_id with the current user but it give me this error

SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a 
default value (SQL: insert into `posts` (`updated_at`, `created_at`) 
values (2017-04-27 10:29:59, 2017-04-27 10:29:59))

here is my method

//PostController
Post::create(request([
        'body' => request('body'),
        'title' => request('title'),
        'user_id' => auth()->id()
    ]));

with these fillables

//Post Model
protected $fillable = ['title', 'body', 'user_id']

However this method do the job

//PostController
auth()->user()->publish(new Post(request(['title' ,'body']))); 

//Post Model
public function publish(Post $post)
{
    $this->posts()->save($post);
}

any idea why this fail? enter image description here

This question is related to php laravel

The answer is


use print_r(Auth);

then see in which format you have user_id / id variable and use it


Here's how I did it:

This is my PostsController

Post::create([

    'title' => request('title'),
    'body' => request('body'),
    'user_id' => auth()->id() 
]);

And this is my Post Model.

protected $fillable = ['title', 'body','user_id'];

And try refreshing the migration if its just on test instance

$ php artisan migrate:refresh

Note: migrate: refresh will delete all the previous posts, users


$table->date('user_id')->nullable();

In your file create_file, the null option must be enabled.


you need to change database strict mode. for disable follow below step

  1. Open config/database.php

  2. find 'strict' change the value true to false and try again


In my case, the error was because I had not declared the field in my $fillable array in the model. Well, it seems to be something basic, but for us who started with laravel it could work.


User Auth::user()->id instead.

Here is the correct way :

//PostController
Post::create(request([
    'body' => request('body'),
    'title' => request('title'),
    'user_id' => Auth::user()->id
]));

If your user is authenticated, Then Auth::user()->id will do the trick.


It's seems flaw's in your database structure. Keep default value None to your title and body column.

Try this:

$user_login_id = auth()->id();
Post::create(request([
        'body' => request('body'),
        'title' => request('title'),
        'user_id' => $user_login_id
    ]));

 Post::create([
      'title' => request('title'),
      'body' => request('body'),
      'user_id' => auth()->id()
    ]);

you dont need the request() as you doing that already pulling the value of body and title


The above error can be as a result of wrongly named input variables from the view form, which resorts to seeking a default value since no variable is supplied hence-- SQLSTATE[HY000]: General error: 1364 Field 'user_id' doesn't have a default value (SQL: insert into posts (updated_at, created_at) values (2017-04-27 10:29:59, 2017-04-27 10:29:59))


There are two solutions for this issue.

First, is to create fields with default values set in datatable. It could be empty string, which is fine for MySQL server running in strict mode.

Second, if you started to get this error just recently, after MySQL/MariaDB upgrade, like I did, and in case you already have large project with a lot to fix, all you need to do is to edit MySQL/MariaDB configuration file (for example /etc/my.cnf) and disable strict mode for tables:

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION

This error started to happen quite recently, due to new strict mode enabled by default. Removing STRICT_TRANS_TABLES from sql_mode configuration key, makes it work as before.


I've had a similar issue with User registration today and I was getting a

SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into users

I fixed it by adding password to my protected $fillable array and it worked

protected $fillable = [
  'name',
  'email',
  'password',
];

I hope this helps.


Use database column nullble() in Laravel. You can choose the default value or nullable value in database.


Try using Auth::id(), like

Post::create([ 'body' => request('body'), 'title' => request('title'), 'user_id' => Auth::id()]);

also remember to include Auth facade at top of your controller which is App\Http\Controllers\Auth


Try

'user_id' => auth()->id
or

'user_id' => Auth::user()->id

instead of

'user_id' => auth()->id()