laravel 函数 Illuminate\Database\Eloquent\Model::setAttribute() 的参数太少,1 个通过

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

Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed

phplaravelauthenticationuuidlaravel-5.6

提问by andri purnama

i want to ask about error "Too few arguments to function Illuminate\Database\Eloquent\Model::setAttribute(), 1 passed "

我想问一下错误“函数 Illuminate\Database\Eloquent\Model::setAttribute(), 1 传递的参数太少了”

i try to register users, i use php artisan make:auth to build my authentication. But, i add one field in users migration. and i try to use uuid. this is my code :

我尝试注册用户,我使用 php artisan make:auth 来构建我的身份验证。但是,我在用户迁移中添加了一个字段。我尝试使用 uuid。这是我的代码:

User Model

用户模型

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
use App\Traits\Uuids;
class User extends Authenticatable
{
use Notifiable;
use Uuids;
/**
 * The attributes that are mass assignable.
 *
 * @var array
 */
protected $guarded = ['id'];
protected $table = 'users';
protected $fillable = [
    'name', 'email', 'password','phone_number',
];


/**
 * The attributes that should be hidden for arrays.
 *
 * @var array
 */
protected $hidden = [
    'password', 'remember_token',
];
}

Register Controller

注册控制器

<?php

namespace App\Http\Controllers\Auth;

use App\User;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Validator;
use Illuminate\Foundation\Auth\RegistersUsers;
use Illuminate\Http\Request;

class RegisterController extends Controller
{
/*
|--------------------------------------------------------------------------
| Register Controller
|--------------------------------------------------------------------------
|
| This controller handles the registration of new users as well as their
| validation and creation. By default this controller uses a trait to
| provide this functionality without requiring any additional code.
|
*/

use RegistersUsers;

/**
 * Where to redirect users after registration.
 *
 * @var string
 */
protected $redirectTo = '/home';

/**
 * Create a new controller instance.
 *
 * @return void
 */
public function __construct()
{
    $this->middleware('guest');
}

/**
 * Get a validator for an incoming registration request.
 *
 * @param  array  $data
 * @return \Illuminate\Contracts\Validation\Validator
 */
protected function validator(array $data)
{
    return Validator::make($data, [
        'name' => 'required|string|max:255',
        'email' => 'required|string|email|max:255|unique:users',
        'phone' => 'required|numeric|min:8',
        'password' => 'required|string|min:6|confirmed',
        'password_confirmation' => 'required|same:password',
    ]);
}

/**
 * Create a new user instance after a valid registration.
 *
 * @param  array  $data
 * @return \App\User
 */
protected function create(array $data)
{
    return User::create([
        'name' => $data['name'],
        'email' => $data['email'],
        'phone_number' => $data['phone'],
        'password' => Hash::make($data['password']),
    ]);
}


}

Users Migration

用户迁移

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateUsersTable extends Migration
{
/**
 * Run the migrations.
 *
 * @return void
 */
public function up()
{
    Schema::create('users', function (Blueprint $table) {
        $table->uuid('id');
        $table->string('name');
        $table->string('email')->unique();
        $table->string('password');
        $table->integer('phone_number');
        $table->rememberToken();
        $table->timestamps();
    });
}

/**
 * Reverse the migrations.
 *
 * @return void
 */
public function down()
{
    Schema::dropIfExists('users');
}
}

Uuids Trait

Uuids 特性

<?php
namespace App\Traits;

trait Uuids 
{
/**
 * 
 */
protected static function boot()
{
    parent::boot();
    static::creating(function ($model) {
        $model->{$model->uuid} = 
        str_replace('-', '', \Uuid::generate(4)->string);
    });
}
}

Please give me advice. Thank You.

请给我建议。谢谢你。

采纳答案by Chay22

I can't find any reference about property or magic property you wrote as below (like a method getUuidAttributefor example)

我找不到任何关于您如下编写的属性或魔法属性的参考(例如方法getUuidAttribute

$model->{$model->uuid} = str_replace('-', '', \Uuid::generate(4)->string);

So, I assume above code would compiled as

所以,我假设上面的代码将编译为

$model->{null} = str_replace('-', '', \Uuid::generate(4)->string);

I guess you're looking for $model->idwhich has the type of database column as a uuid. But, obviously, you can't have a dynamic database column name as it is wrong at all. Example of dynamic column if your snippet is working properly

我猜您正在寻找$model->id哪个数据库列的类型为uuid. 但是,显然,您不能拥有动态数据库列名,因为它根本是错误的。如果您的代码段工作正常,则动态列的示例

$model->'6b324d55-433c-455a-88b6-feb2ee6c3709' = 'c3bec822-e5ee-4d91-b1a3-f5f552fd004a';

Something isn't right, right? It would be better if it look like this.

有什么不对的,对吧?如果它看起来像这样会更好。

$model->id = 'c3bec822-e5ee-4d91-b1a3-f5f552fd004a';

And of course, make your latest snippet be something as follow

当然,让你的最新片段如下

<?php
namespace App\Traits;

trait Uuids {

    // to make model run this 'boot' method, append it with your trait name
    protected static function bootUuids() { // <-- bootUuids
        // parent::boot();

        static::creating(function ($model) {
            $model->id = str_replace('-', '', \Uuid::generate(4)->string);
        });
    }
}

回答by Pawan Kumar

This error is occurred because of the following

发生此错误的原因如下

$data = ['user_id' => 1,'content' => 'content'];
$data = User::create($data);

in this condition, this error occurs if our array variable name is data and create $data = User::create($data); is data hereafter create laravel return data object and then both of data it gets ambiguous so it throws an error.

在这种情况下,如果我们的数组变量名是 data 并且 create $data = User::create($data); 是数据此后创建laravel返回数据对象,然后两个数据都变得不明确,因此会引发错误。