37

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

Martijn
  • 15,791
  • 4
  • 36
  • 68
Mohammed Sabbah
  • 873
  • 1
  • 8
  • 14

15 Answers15

49

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

Shailesh Ladumor
  • 7,052
  • 5
  • 42
  • 55
33

So, after After reviewing the code again, I found the error. I am wondering how no one notice that! In the above code I wrote

Post::create(request([  // <= the error is Here!!!
    'body' => request('body'),
    'title' => request('title'),
    'user_id' => auth()->id()
]));

actually, there is no need for the request function warping the body of the create function.

// this is right
Post::create([
    'body' => request('body'),
    'title' => request('title'),
    'user_id' => auth()->id()
]);
Martijn
  • 15,791
  • 4
  • 36
  • 68
Mohammed Sabbah
  • 873
  • 1
  • 8
  • 14
32

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.

Lamin Barrow
  • 839
  • 10
  • 16
14

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

Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
Mayur Budukale
  • 239
  • 3
  • 5
  • 2
    Adding the `user_id` to fillable will solve the problem if you don't have a syntax error in the controller, but why are you refreshing the migration? – Salam Jul 07 '18 at 10:46
3

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

Community
  • 1
  • 1
Sapnesh Naik
  • 11,011
  • 7
  • 63
  • 98
3

Try

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

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

instead of

'user_id' => auth()->id()
Calin Blaga
  • 1,375
  • 12
  • 19
3

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.

Ilia Ross
  • 13,086
  • 11
  • 53
  • 88
2

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.

Gammer
  • 5,453
  • 20
  • 78
  • 121
1

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.

Alejandro Díaz
  • 430
  • 4
  • 10
0

use print_r(Auth);

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

Muhammad Hassaan
  • 7,296
  • 6
  • 30
  • 50
  • Dumping the whole auth will consume all the memory, if you want to dump the `user_id` do `print_r(auth()->id())` – Salam Jul 07 '18 at 10:42
0

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
    ]));
Muhammad Rizwan
  • 488
  • 1
  • 7
  • 23
0
 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

Abhishek Pakhare
  • 476
  • 1
  • 4
  • 15
0

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

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

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

Das_Geek
  • 2,775
  • 7
  • 20
  • 26
  • updating my column to nullable was the only thing that worked in my case. You can do this by creating a new migration and updating the problematic column using a code very similar to the one above. Just add ->change() to the end. E.g `$table->date('user_id')->nullable()->change();` – Eaweb Jul 10 '21 at 01:51
-1

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

JJJ
  • 32,902
  • 20
  • 89
  • 102
Dilshan Dilip
  • 707
  • 6
  • 9