我想问一下错误“函数Illuminate \ Database \ Eloquent \ Model :: setAttribute()的参数太少,传递了1个”
我尝试注册用户,我使用php artisan make:auth来建立我的身份验证。但是,我在用户迁移中添加了一个字段。我尝试使用uuid。这是我的代码:
用户模型
<?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',
];
}
注册控制器
<?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']),
]);
}
}
用户迁移
<?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特性
<?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);
});
}
}
请给我建议。谢谢。
答案 0 :(得分:1)
由于以下原因而发生此错误
$data = ['user_id' => 1,'content' => 'content'];
$data = User::create($data);
在这种情况下,如果我们的数组变量名称为data并创建$ data = User :: create($ data);,则会发生此错误。是数据,此后创建laravel返回数据对象,然后两个数据都变得模棱两可,因此引发错误。
答案 1 :(得分:0)
我找不到与您如下所述的属性或魔术属性有关的任何引用(例如方法getUuidAttribute
)
$model->{$model->uuid} = str_replace('-', '', \Uuid::generate(4)->string);
所以,我假设上面的代码将编译为
$model->{null} = str_replace('-', '', \Uuid::generate(4)->string);
我猜您在寻找$model->id
,其数据库列的类型为uuid
。但是,显然,您不能拥有动态数据库列名称,因为它根本是错误的。如果您的代码片段正常运行,则为动态列示例
$model->'6b324d55-433c-455a-88b6-feb2ee6c3709' = 'c3bec822-e5ee-4d91-b1a3-f5f552fd004a';
有些不对,对吧?看起来像这样会更好。
$model->id = 'c3bec822-e5ee-4d91-b1a3-f5f552fd004a';
当然,请按照以下步骤操作最新的代码段
<?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);
});
}
}