Laravel 5.4MassAssignmentException in Model.php line 225: _token

Published 1 week ago by carlek

I was making a system wherein only administrator can adding users. I have a problem, becouse after filling the form I receives error who looks like : MassAssignmentException in Model.php line 225: _token

My Form:

           {!! Form::open(['url'=>'adminpanel/adduser/store','class'=>'form-horizontal']) !!}
            <div class="form-group">
                <div class="col-md-4 control-label">
                    {!! Form::label('nameandsurname','imie i nazwisko:') !!}
                </div>
                <div class="col-md-6">
                    {!! Form::text('nameandsurname', null, ['class' => 'form-control']) !!}
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-4 control-label">
                    {!! Form::label('PESEL','PESEL:') !!}
                </div>
                <div class="col-md-6">
                    {!! Form::text('PESEL',null,['class'=>'form-control']) !!}
                </div>
            </div>

            <div class="form-group">
                <div class="col-md-4 control-label">
                    {!! Form::label('adress','adres:') !!}
                </div>
                <div class="col-md-6">
                    {!! Form::text('adress',null,['class'=>'form-control']) !!}
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-4 control-label">
                    {!! Form::label('position','stanowisko:') !!}
                </div>
                <div class="col-md-6">
                    {!! Form::text('position',null,['class'=>'form-control']) !!}
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-4 control-label">
                    {!! Form::label('email','email:') !!}
                </div>
                <div class="col-md-6">
                    {!! Form::text('email',null,['class'=>'form-control']) !!}
                </div>
            </div>
            <div class="form-group">
                <div class="col-md-4 control-label">
                    {!! Form::label('leavesdays','liczbadniwolnych:') !!}
                </div>
                <div class="col-md-6">
                    {!! Form::text('leavesdays',null,['class'=>'form-control']) !!}
                </div>
            </div>


            {!! Form::hidden('password', null) !!}
            {!! Form::hidden('remember_token',null) !!}

This is User Model :

        use Notifiable;
    
protected $fillable = [
    'nameandsurname', 'email','PESEL','adress','position','leavesdays',

];
protected $table = 'users';

protected $hidden = [
 'password', 'remember_token',
];

Request in controller :

       public function store(AddUserRequest $request)
       {
      //        User::create($request->except('_token'));
$newUser = User::create($request->all());
$newUser->save();

return redirect('/adminpanel/admindashboard');

} Migration:

        public function up()
    {
        Schema::create('users', function (Blueprint $table) {
        $table->increments('id');
        $table->string('nameandsurname',40);
        $table->string('PESEL',9);
        $table->string('adress',30);
        $table->enum('position',array('employer','worker','accountant'));
        $table->string('email',100)->unique();
        $table->string('password');
        $table->integer('leavesdays');
        $table->rememberToken();
        $table->timestamps();
        });
    }

I was looking for this problem on the internet, but any suggestion helped me.

Best Answer (As Selected By carlek)
mdecooman

@carlek

Your user model is wrong You should use the correct model of your user which is

Use App\User;

//and not "use Illuminate\Foundation\Auth\User;"
jbowman99

@carlek

try this in your User::create($request->except('_token'));

carlek

@jbowman99 I tried it, That didnt solve the problem.

jbowman99

@carlek

whats being sent in the request?

dd($request->all());

mdecooman

@carlek

try

$request->offsetUnset('_token');

source: here

carlek

@jbowman99

array:9 [▼ "_token" => "bWf3WcdXGY2uDqx5z9vBVO5Fg3MuBHG2BPXQFth4" "nameandsurname" => "sdadsa" "PESEL" => "dsadas" "adress" => "dsadsa" "position" => "saddsadsa" "email" => "sdadsa" "leavesdays" => "adsdsadsadsa" "password" => null "remember_token" => null ]

somnathsah

Try not sending hidden field password and remember_token if that might be an issue or you can add these fields also into the fillable array

carlek

@mdecooman

FatalThrowableError in Builder.php line 732: Type error: Argument 1 passed to Illuminate\Database\Eloquent\Builder::create() must be of the type array, null given, called in C:\Users\sekre\urlopy\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php on line 1329

mdecooman

@carlek

can you copy/paste here the use statements of your controller?

Other thing, can you comment your $fillable and see if it works when you do instead (just to check that the issue is where we think it is)

$guarded = []
carlek

@mdecooman The same when i comment my @fillable : MassAssignmentException in Model.php line 225: _token

What do You mean at this sentence : "can you copy/paste here the use statements of your controller?". Sorry, my english is so bad :( Do you want me to copy all lines of the controller?

mdecooman

@carlek yes (to make it short ;-)

carlek

@mdecooman

   namespace App\Http\Controllers;

    use Illuminate\Foundation\Auth\User;
    use Illuminate\Http\Request;
    use App\Http\Requests\AddUserRequest;

     class HomeController extends Controller
    {

     public function __construct()
    {
    $this->middleware('auth');
     }


     public function index()
    {
    return view('adminpanel.admindashboard');
    }
    public function create()
   {
    return view('adminpanel.adduser');
   }
    public function store(AddUserRequest $request)
   {
    //        User::create($request->except('_token'));
    //User::create(['nameandsurname'=>'some_value', 
    'PESEL'=>'098','adress'=>'blablabla','position'=>'employer','email'=>
      'sampleE
      [email protected]','leavesdays'=>'27']);
      //User::create($request->only((new User())->getFillable())-
      >except('_token'));
    //$newUser = User::create($request->all()->offsetUnset('_token'));
    // User::create($request->all());
    dd($request->only((new User())->getFillable()));
    return redirect('/adminpanel/admindashboard');
    }
     }
mdecooman

@carlek

Your user model is wrong You should use the correct model of your user which is

Use App\User;

//and not "use Illuminate\Foundation\Auth\User;"
jekinney
jekinney
1 week ago (216,825 XP)

If you use create(); don't use save() too. That is what's causing your issue.

carlek

@jekinney

I know now, but after delete this nothing has changed.

@mdecooman

It looks better now :

QueryException in Connection.php line 647:
SQLSTATE[HY000]: General error: 1364 Field 'password' doesn't have a default value (SQL: insert into `users` (`nameandsurname`, `PESEL`, `adress`, `position`, `email`, `leavesdays`, `updated_at`, `created_at`) values (sdadas, dadsa, adsadsa, dsadsaads, dssadsadas, dsadsadsa, 2017-04-18 21:26:58, 2017-04-18 21:26:58))```



mdecooman

@carlek

It is because you need to add the 'password' too in your $fillable,

protected $fillable = [
    'password', 'nameandsurname', 'email','PESEL','adress','position','leavesdays',

];

FYI, protected $hidden is only hiding these fields when returning your model's array or JSON representation.

Sign In or create a forum account to participate in this discussion.