Call to undefined method Illuminate\Database\Query\Builder::role()
<p>I get this error in my view when i tried to check whether the user has some role. The exact error is 
<strong>Call to undefined method Illuminate\Database\Query\Builder::role() (View: C:\xampp\htdocs\laravel-projects\acl\resources\views\admin.blade.php</strong></p>

<p>Here is the admin.blade.php code</p>

<pre><code>@foreach($users as $user)
&lt;tr&gt;

&lt;td&gt;{{$user-&gt;name}}&lt;/td&gt;
&lt;td&gt;{{$user-&gt;email}}&lt;/td&gt;
&lt;td&gt;&lt;input type="checkbox" {{ $user-&gt;hasRole('User') ? 'checked' : '' }} name="role_user"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="checkbox" {{ $user-&gt;hasRole('Author') ? 'checked' : '' }} name="role_author"&gt;&lt;/td&gt;
&lt;td&gt;&lt;input type="checkbox" {{ $user-&gt;hasRole('Admin') ? 'checked' : '' }} name="role_admin"&gt;&lt;/td&gt;
&lt;td&gt;{{$user-&gt;name}}&lt;/td&gt;
&lt;td&gt;{{$user-&gt;email}}&lt;/td&gt;
&lt;/tr&gt;
@endforeach
</code></pre>

<p><strong>hasRole()</strong> is a function in the user model. But from the error it throws undefined methode role(). But there is no method called role()</p>

<p>Here is my controller </p>

<pre><code>class AdminController extends Controller
{

public function index()
{   
$user_details = User::all();
return view('admin')-&gt;with('users' , $user_details);
}    
}
</code></pre>

<p>I also did include the method while getting the data from DB like </p>

<pre><code>$user_details = User::with('hasRole')-&gt;get();
</code></pre>

<p>Even this didn't work.</p>

<p>This is my User model.</p>

<pre><code>&lt;?php

namespace App;
use App\Role;

use Illuminate\Foundation\Auth\User as Authenticatable;

class User extends Authenticatable
{

public function roles()
{
    return $this-&gt;belongsToMany('App\Role','user_role','user_id','role_id');
}

public function hasAnyRole($roles)
{
    if(is_array($roles)){

        foreach ($roles as $role) {

            if ($this-&gt;hasRole($role)) {
                return true;
            }
        }
    }
    else{

        if ($this-&gt;hasRole($roles)) {

            return true;
        }
    }
    return false;
}

public function hasRole($role)
{
    if ($this-&gt;role()-&gt;where('name',$role)-&gt;first()) {

        return true;
    }

    return false;
}
}
</code></pre>