jeudi 2 avril 2020

Trying to get property 'id' of non-object: Laravel 6

Try to create a form where a user can choose a role for their account i.e Author,Editor,Reader. I encounter this error when trying to pull my role id/name from the database. Any ideas why? I will try include everything i think is connected to this as I've been looking for a while and cant figure out why. Thank you

view throwing problem

    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">
                <strong>Edit 's Profile</strong></div>
                <div class="card-body">
                <form action="" method="post" enctype="multipart/form-data">
                
                <div class="form-group">
                    <label for="name">Name</label>
                    <input type="text" name ="name" value="" class="form-control">
                </div>
                <div class="form-group">
                    <div class="text-centre">
                        <button class="btn btn-success" type="submit">
                            Save Changes
                        </button>
                    </div>
                    <p></p>
                </div>
            </div>
        </div>
    </div>
</div>
```

UserController


```public function edit(User $user)
    {
        $user = Auth::user();
        $roles = Role::all();
        return view('admin.users.choose_role')->with(['user' => '$user', 'roles' => '$roles']);
    }
```
in the namespace is included:
```
use App\User;
use App\Role;
use Auth;
```
Then my route: 
```
Route::get('/users/edit/{id}', 'UserController@edit')->name('users.edit')
```
 i will add my Role Model, which has the relationship to the user table defined in it.
```
class Role extends Model
{
    public function users(){
        return $this->belongsToMany('App\User', 'user_role');
    }

}
```
And my user model
```
class User extends Authenticatable
{
    use Notifiable;


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


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


    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

    public function roles(){
        return $this->belongsToMany('App\Role', 'user_role');
    }
}
```
Any ideas guys? thanks



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire