mercredi 31 mai 2017

Php Laravel Check If User Has Attribute Middleware

I need to check if a user has been accepted into the application (not simply authenticated) before allowing them access into a route group.

I have a middleware for this:

 public function handle($request, Closure $next)
    {
        if(!$request->user()->applicationAccepted() ){
            return redirect('/application/status');
        }
        return $next($request);
    }

My problem is I want administrators, who don't have applications to accept, to be allowed into this section. That is, they shouldn't see application/status.

My user model:

public function application()
    {
        return $this->hasOne('App\Models\Application','user_id');
    }

    public function applicationAccepted()
    {

    if($this->isAdmin){ //adminscansneakbywithoutapplications
        return true;
    }

        return $this->application->accepted;
    }

    public function isAdmin(){
        return $this->hasOne('App\Models\Admin','user_id');
    }

My problem is applicationAccepted() always stops the admins, even those who do have a isAdmin() relation. What is the proper way to query this hasOne relation within the very same user model?

Thank you.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire