php 一般错误:1364 字段“user_id”没有默认值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43651446/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:53:38  来源:igfitidea点击:

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

phplaravel

提问by Mohammed Sabbah

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

我正在尝试为当前用户分配 user_id,但它给了我这个错误

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

知道为什么会失败吗? 在此处输入图片说明

采纳答案by Mohammed Sabbah

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()
]);

回答by Shailesh Ladumor

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

  1. 打开 config/database.php

  2. 找到'strict'将值 true 更改为 false 并重试

回答by Mayur Budukale

Here's how I did it:

这是我如何做到的:

This is my PostsController

这是我的 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

注意:迁移:刷新将删除所有以前的帖子,用户

回答by Lamin Barrow

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`

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

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

我通过添加password到受保护的 $fillable 数组来修复它并且它起作用了

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

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

I hope this helps.

我希望这有帮助。

回答by Ilia Rostovtsev

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.

首先,是使用数据表中设置的默认值创建字段。它可以是空字符串,这适用于在严格模式下运行的 MySQL 服务器。

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:

其次,如果你刚刚开始遇到这个错误,就像我一样,在 MySQL/MariaDB 升级之后,如果你已经有很多需要修复的大项目,你需要做的就是编辑 MySQL/MariaDB 配置文件(例如/etc/my.cnf)并禁用表的严格模式:

[mysqld]
sql_mode=NO_ENGINE_SUBSTITUTION

This error started to happen quite recently, due to new strict mode enabled by default. Removing STRICT_TRANS_TABLESfrom sql_modeconfiguration key, makes it work as before.

由于默认启用了新的严格模式,此错误最近开始发生。STRICT_TRANS_TABLESsql_mode配置键中删除,使其像以前一样工作。

回答by Sapnesh Naik

Try using Auth::id(), like

尝试使用Auth::id(),比如

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

还记得在你的控制器顶部包含 Auth 门面,它是 App\Http\Controllers\Auth

回答by Calin Blaga

Try

尝试

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

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

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

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

instead of

代替

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

回答by Gammer

User Auth::user()->idinstead.

用户Auth::user()->id代替。

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()->idwill do the trick.

如果您的用户已通过身份验证,则Auth::user()->id可以解决问题。

回答by Alejandro Díaz

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.

就我而言,错误是因为我没有在模型中的 $fillable 数组中声明该字段。嗯,这似乎是一些基本的东西,但对于我们从 laravel 开始的它可以工作。

回答by Muhammad Rizwan

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

您的数据库结构似乎存在缺陷。将默认值 None 保留到您的标题和正文列。

Try this:

尝试这个:

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