Laravel:静态::创建与 DB:插入

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27807182/
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-09-14 10:39:05  来源:igfitidea点击:

Laravel: static::create vs DB:insert

phpdatabaselaraveleloquent

提问by Prezioso

If I use the following code snippet in my model it inserts the data:

如果我在我的模型中使用以下代码片段,它会插入数据:

$instance = DB::table('users')->insert(compact('email', 'username'));

But if I do this instead:

但如果我这样做:

$instance = static::create(compact('email', 'username'));

It inserts null, but created_at and updated_at are inserted.

它插入空值,但插入了 created_at 和 updated_at。

回答by ceejayoz

Laravel's created_at/updated_atare part of Illuminate\Database\Eloquent\Model. A raw DB::tablequery builder isn't an Eloquent model and thus doesn't have those automatic parameters.

Laravel 的created_at/updated_atIlluminate\Database\Eloquent\Model. 原始DB::table查询构建器不是 Eloquent 模型,因此没有那些自动参数。

NULLdata is being inserted in the Eloquent query because Eloquent has a $fillableparameter you need to define. This parameter sets which columns can be mass-assigned. Laravel strips attributes not in this array when you do a fill, create, or otherwise instantiate a new object. In your model, you'd want:

NULL数据被插入到 Eloquent 查询中,因为 Eloquent 有一个$fillable你需要定义的参数。此参数设置可以批量分配哪些列。Laravel不去掉这个属性阵列中,当你做一个fillcreate或以其他方式实例化一个新的对象。在您的模型中,您需要:

class User extends Eloquent {
  protected $fillable = ['email', 'username'];
}

回答by jamadri

ceejayoz answer is great, here's an explanation of how to call the model. Once the model is created, let's say this model:

ceejayoz 的回答很棒,这里有关于如何调用模型的解释。创建模型后,让我们说这个模型:

class User extends Eloquent {
  protected $fillable = ['email', 'username'];
}

Then you need to call by using the model directly and eloquents ORM like so:

然后你需要直接使用模型调用并像这样雄辩ORM:

// example is this. True method is TableName->ColumnName = Value;
$user = User::find($id);
$user->username = '';
$user->fullname = '';
$user->save();

The save will update the columns based on what you describe. With this you don't even need the fillable variable.

保存将根据您的描述更新列。有了这个,您甚至不需要可填充变量。

Some other variables for models that are good to know is:

值得了解的模型的其他一些变量是:

protected $primaryKey = 'userid';   #if you have an primarykey that isn't named id
protected $table = 'tablename';     #if table name isn't pulling by the name of the model
protected $timestamps = true;       #bool value, whether u have timestamp columns in table

回答by DouglasDC3

You said us that you done

你说我们你做到了

class User extends Eloquent {
  protected $fillable = ['email', 'username'];
}

however in your comment you told us you got the following error in your apache log

但是在您的评论中,您告诉我们您的 apache 日志中出现以下错误

SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry '' 
for key 'users_email_unique' (SQL: insert into ``users 
(updated_at, created_at``) values (2015-01-06 21:52:35, 2015-01-06 21:52:35))

Make sure to also include users_email_uniquein your fillable array if you want to set it.

users_email_unique如果要设置它,请确保也包含在可填充数组中。

class User extends Eloquent {
  protected $fillable = ['email', 'username', 'users_email_unique'];
}