Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

vipin93's avatar
Level 13

how to make middleware for relationship?

I make another guard for student login which belongs to User who created. now problem with me that how can request Student->users->active in middleware so that if ($student->users->active ==1) then proceed else 404

here is my Student model


<?php

namespace App;

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

class Student extends Authenticatable
{
    use Notifiable;

    protected $fillable = [
        'name', 'reg_no', 'password','ppactive'
    ];

    protected $guard = 'student';

    public function owner()
    {
        return $this->belongsTo(User::Class,'user_id');
    }

    protected $hidden = [
        'password', 'remember_token',
    ];
}

and my User Model

<?php

namespace App;

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

class User extends Authenticatable
{
    use Notifiable;

    
    protected $fillable = [
        'name', 'email', 'password','active','plan'
    ];

     public function students()
    {
        return $this->hasMany(Student::Class);
    }

    
    protected $hidden = [
        'password', 'remember_token',
    ];
}

and middleware

public function handle($request, Closure $next)
    {
        // $student = Auth::user();

        // if ($student->users()->active == 1) 
        // {
        //      return $next($request);
        // }else{

        // flash('Sorry! You Do not have permission.', 'danger');

        //   return redirect('/oops');
        // }
       
    }

thanks

0 likes
6 replies
jacurtis's avatar

In your middleware, you start by setting $student equal to the current user. Next, in your if statement you use $student (which is a User object) and call the users() relationship on it which can not happen, because users() is a relationship of the Student model (not the User model, which is what you currently have $student equal to).

Based on what it looks like you are doing is, you want to see if the currently logged in user is an active user, right?

If that is the case, you can get rid of the whole $student = Auth::user() thing altogether. Just call Auth::user()->active == 1 in your if statement and you should be good to go.

public function handle($request, Closure $next)
{
    if (Auth::user()->active == 1) {
        return $next($request);
    }else{
        flash('Sorry! You Do not have permission.', 'danger');
        return redirect('/oops');
    }
}
3 likes
vipin93's avatar
Level 13

no i want if a Current student (owner which in case users should be active if owner of student not active then he not allow to see that page)

SachinAgarwal's avatar

@vipin93 that is exactly what @jacurtis said.

Auth::user() gives you current Login User. Who will have many students. Thus checking for Auth::user()->active == 1 gives you if the current login is user is active or not.

If your definition of current student is not current login user then please give a clarity about what is current student.

vipin93's avatar
Level 13

@SachinAgarwal as u can see in I'm using student guard for my Student so in this my case my current user is Student not User

public function handle($request, Closure $next)
    {
        // $student = Auth::guard('student'); here i want to request cureent student 

        // if ($student->owner()->active == 1) 
        // {
        //      return $next($request);
        // }else{

        // flash('Sorry! You Do not have permission.', 'danger');

        //   return redirect('/oops');
        // }
       
    }

i updated my question

vipin93's avatar
Level 13

its look like i have to make another use this is for only User model

Illuminate\Foundation\Auth\User as Authenticatable;

so should i have another trait for Student like

Illuminate\Foundation\Auth\Student as AuthenticatableStudent;

but problem how can make there is lots of method

vipin93's avatar
vipin93
OP
Best Answer
Level 13

ye i solved that

public function handle($request, Closure $next)
    {
        
        $student = $request->user();

        if ($student && $student->owner->active == 1) 
        {
             return $next($request);

        }

        flash('Sorry! You Do not have permission.', 'danger');

          return redirect('/oops');
       
    }

Please or to participate in this conversation.