7 months ago

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

Posted 7 months 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>

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

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;}
           {$us->admin = 0;}

         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


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.