Search code examples
phplaravelauthenticationuuidlaravel-5.6

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


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

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 :

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

<?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.


Solution

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

    $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->id which 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->'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);
            });
        }
    }