laravelmigrationlaravel-5.8mass-assignmentlaravel-models

How to resolve mass assignment error in laravel?


I am new to the laravel, i am implementing user registration api while registering the user it's throwing an error like mass_assignment

Add [first_name] to fillable property to allow mass assignment on [App\\Entities\\User].

User.php

<?php

namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{
    use Notifiable;

    protected $table ='user';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'first_name','last_name','phone_number','email'
    ];

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

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
}

UserController.php

 public function userRegister(userRequest $request){
        $data = $this->repository->create($request->all());     
        return $user = new UserTransformer($data);                     
    }
}

please help why this error is happening ..?


Solution

  • The main problem is laravel5.8 the model paths are different so use following facade

    use App\modelname
    

    instead of use App\Entities\User,let me know if this resolves your problem :-)