Reputation: 1235
I´m having this error when i call my method in blade.
My Model
class User extends Authenticatable{
use Notifiable;
public function roles(){
return $this->belongsToMany('App\Role', 'user_roles', 'user_id', 'role_id');
}
public function hasAnyRole(){
if(is_array($roles)){
foreach ($$roles as $role) {
if($this->hasRole($role)){
return true;
}
}
}else{
if($this->hasRole($roles)){
return true;
}
}
return false;
}
public function hasRole($role){
if($this->roles()->where("name", $role)->first())
return true;
else
return false;
}}
Then in my home.blade.php
@foreach($users as $user)
{{ $user->hasRole("Admin") }}
<tr>
<form action="{{ route("admin.assignrole") }}" method="get" accept-charset="utf-8">
{{ csrf_field() }}
<td>{{ $user->name }}</td>
<td>{{ $user->email }}</td>
<td><input type="checkbox" name="admin" value="admin"></td>
<td><input type="checkbox" name="cultura" value="cultura"></td>
<td><input type="checkbox" name="opinion" value="opinion"></td>
<td><button type="button" class="btn btn-primary">Asignar</button></td>
</form>
</tr>
@endforeach
But i´m getting: "FatalErrorException in 4ec2afb0bd7b9558404eeeb89a6d9200e0c5639a.php line 18: Call to undefined method stdClass::hasRole()"
I cant figure why;
Upvotes: 4
Views: 15296
Reputation: 1235
Thank's to CBroe´s help i figured out what´s the problem. is that im using query builder to get my users to pass it to my view, now i´m using Eloquent Model and all works Ok
Solution: In my controller class:
//$users = DB::table('users')->get();//Query Builder
$users = User::all();//Eloquent
return view('admin.home', compact("users"));
thanks for the help
Upvotes: 2