jim1506
1 week ago
388
6
Laravel

Getting a Attempt to assign property 'admin' of non-object when adding to Users

Posted 1 week ago by jim1506

I have amended the users table to add an integer field admin. I have a form which includes a checkbox for this:

              <div class="form-group col-xs-12">
                  <div class="checkbox ">
                      <input type="checkbox" name="admin" id="checkbox_372773_0" value="1" data-alias="" checked=""><label for="checkbox_372773_0" class="checkbox-inline">
                      &nbsp;&nbsp;admin </label>
                  </div>

                  <span id="checkbox_372773"></span>
              </div>

and in my controller I have a store method:

public function store(Request $request)
    {
         
         $us = new User;

         $us->name = $request->name;
         $us->email = $request->email;
         $us = Hash::make($request->password);

         if (isset($request->admin))
           { $us->admin = 1;}
         else
           {$us->admin = 0;}

         $us->save();
         session()->flash('success','the user has been added');
         return redirect()->route('users');
    }

if I run this I get the error

Attempt to assign property 'admin' of non-object

so I added

dd($request')

to check the incoming request

request: ParameterBag {#44 ▼
    #parameters: array:6 [▼
      "_token" => "TNaQs6HAwGUbz87PNRJoOeZcurIJgKbKnbEaAbWX"
      "name" => "Jim"
      "email" => "[email protected]"
      "password" => "aPassword"
      "admin" => "1"
      "button_569082" => null
    ]
  }

I have added the field to the fillable on the User.php:

protected $fillable = [
        'name', 'email', 'password','admin',
    ];

but it has made no difference, and of course I have not got to the store part of the controller method.

Please sign in or create an account to participate in this conversation.