Laravel First 或 New

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

Laravel First Or New

phplaraveleloquent

提问by Brent

I seem to be getting the same error when I use UpdateOrNew or FirstOrNew in Laravel, to the best of my knowledge I have implemented the code correctly.

当我在 Laravel 中使用 UpdateOrNew 或 FirstOrNew 时,我似乎遇到了同样的错误,据我所知,我已经正确地实现了代码。

Current Code

当前代码

    $updateOrCreate = Rating::firstOrNew(
        array(
            'user_id' => Auth::user()->id,
            'video_id' => $_POST['videoId']
            )
    );

    $updateOrCreate->user_id = Auth::user()->id;
    $updateOrCreate->video_id = $_POST['videoId'];
    $updateOrCreate->rating =   $_POST['rating'];

    if($updateOrCreate->save()){
        echo "saved";
    }else{
        echo "failed";
        print_r($_POST);
    };

Error

错误

error: {type:Illuminate\Database\Eloquent\MassAssignmentException, message:user_id,…}
file: "/home/celeb/public_html/dev/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php"
line: 411
message: "user_id"
type: "Illuminate\Database\Eloquent\MassAssignmentException"

回答by Joe

You need to enable mass assignment in your model as such:

您需要在模型中启用质量分配,如下所示:

class User extends Eloquent {

    protected $fillable = array('first_name', 'last_name', 'email');

}

So any field that can be mass assigned should be in the $fillable variable.

所以任何可以批量分配的字段都应该在 $fillable 变量中。

Assigning values without mass assignment:

分配没有质量分配的值:

$user = new User;
$user->id = 3;
$user->name = "Joe";

$user->save();

Assigning values withmass assignment:

使用质量分配分配值:

$userDetails = array('id' => 3, 'name' => 'Joe');
$user = User::create($userDetails);

回答by MaXi32

"You may also use the create method to save a new model in a single line. The inserted model instance will be returned to you from the method. However, before doing so, you will need to specify either a fillable or guarded attribute on the model, as all Eloquent models protect against mass-assignment". [See Laravel Documentation on Eloquent Insert, Update, Delete ] (http://laravel.com/docs/5.0/eloquent#insert-update-delete)

“您也可以使用 create 方法将新模型保存在一行中。插入的模型实例将从该方法返回给您。但是,在此之前,您需要在模型,因为所有 Eloquent 模型都可以防止大规模分配”。[请参阅有关 Eloquent 插入、更新、删除的 Laravel 文档] ( http://laravel.com/docs/5.0/eloquent#insert-update-delete)

That means, only the create method can protect your codes from mass assignment:

这意味着,只有 create 方法可以保护您的代码免受大量分配:

/*$user=*/ User::create(array( 'name' => 'Max', 'email' => '[email protected]' ));

/*$user=*/ User::create(array( 'name' => 'Max', 'email' => '[email protected]' ));

When using the create method, you specify the model's name which is User from the example above. This model (usually User.php) is the place where you assign the mass assignable variables:

使用 create 方法时,您指定模型的名称,即上例中的 User。这个模型(通常是 User.php)是你分配大量可分配变量的地方:

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

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