There are two templates in the first one, everything is displayed normally. The second error is:

Trying to get property 'name' of non-object

With what it can be connected ?

The first controller UserController.php

public function index() { return view('user_managment.user.index',[ 'users' => User::paginate(10) ]); } 

first template index.blade.php

 {{$user->roles->name}} 

second controller RoleController.php

 public function create() { return view('role_managment.role.create',[ 'role' => '', 'users' => User::all() ]); } 

second template create.blade.php

 @foreach ($users as $user) {{$user->name}} {{$user->roles->name}} {{-- @isset($user->roles->name) пользователя уже имеет роль @endisset --}} </label> <br> @endforeach 

Role model

  public function roles() { return $this->belongsTo('App\Specialty','role','id'); } 

User Model

 public function user() { return $this->hasMany('App\User','role','id'); } 
  • How do you describe the relationship between User and Role? - P. Fateev
  • Added to the question description these models - Alexey Bachkov

1 answer 1

The error was that in one case the wrong model is used, at

 use Illuminate\Foundation\Auth\User; 

And the relationship was made to

 use App\User;