0
votes

am new to Laravel

I have set up permissions and roles inside my application, and assigned these to users - however when I try to use hasRole or hasAnyRole it isn't working for me.

Here is my middleware:

        namespace App\Http\Middleware;

        use Closure;
        use Auth;
        class AccessAdmin
        {
            /**
             * Handle an incoming request.
             *
             * @param  \Illuminate\Http\Request  $request
             * @param  \Closure  $next
             * @return mixed
             */
            public function handle($request, Closure $next)
            {
                if(Auth::user()->hasRoles('Admin')){
                    return $next($request);
                }
                return redirect('home');
            }
        }

    <?php

Role Model

namespace App;

use Illuminate\Database\Eloquent\Model;

class Role extends Model
{
    public function users(){
        return $this->belongsToMany('App\User');
    }
}
User Model
<?php

namespace App;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;

class User extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $fillable = [
        'name', 'email', 'password',
    ];

    /**
     * The attributes that should be hidden for arrays.
     *
     * @var array
     */
    protected $hidden = [
        'password', 'remember_token',
    ];

    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];
    public function roles(){
        return $this->belongsToMany('App\Role');
    }
    public function hasAnyRoles($roles){
        return null !== $this->roles()->whereIn('name',$roles)->first();
    }
    public function hasRoles($role){
        return null !== $this->roles()->where('name',$role)->first();
    }
}

which is producing this error:

FatalThrowableError (E_ERROR) Call to a member function hasRole() on null

1
Well, do you have an authenticated user?Daan
not sure if i understand quite your answer but i added Admin and User in database using seedana bel
Auth::user() is null. Are you logged in?Tim Lewis
im logged as admin when i removed the middleware it worked however when i added it it gave me the errorana bel
can you show the route definition for the route you are attempting to reach that is causing the issuelagbox

1 Answers

1
votes

If there is no logged in user then Auth::user() is null, try the following

<?php 

namespace App\Http\Middleware;

use Closure;
use Auth;
class AccessAdmin
{
    /**
        * Handle an incoming request.
        *
        * @param  \Illuminate\Http\Request  $request
        * @param  \Closure  $next
        * @return mixed
        */
    public function handle($request, Closure $next)
    {
        if(!Auth::guest() && Auth::user()->hasRoles('Admin')){
            return $next($request);
        }
        return redirect('home');
    }
}

This middleware does the same as your original but it checks to make sure the user is logged in first. If they aren't logged in, or if they aren't admins, it will redirect home.