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

FounderStartup's avatar

What is the correct way to use relation in query ?

'User' has one to many relation with 'Follower'.

I need to send email to all the followers of the auth user. what will be the correct query ?

User Model

    public function followers()
    {
        return $this->hasMany(Follower::class, 'brokerid', 'id');
    }

Follower Model

    public function broker(){
        return $this->belongsTo(User::class,'id','brokerid');
    }

    public function follower(){
        return $this->belongsTo(User::class,'id','followerid');
    }

Controller :

       $allusers = User::where('id', '<>', Auth::id() )
                ->where('status' , 1)
                ->whereRoleIs('broker')
                ->where('email_verified_at', '<>', NULL)
                ->where('mailforfollowers', 1)
                ->get();

Any help ?

0 likes
10 replies
anilkumarthakur60's avatar
$allusers = User::where('id', '<>', Auth::id() )
                ->where('status' , 1)
                ->whereRoleIs('broker')
                ->where('email_verified_at', '<>', NULL)
                ->where('mailforfollowers', 1)
				->whereHas('broker',function($query) user ($request){
			$query->where();//query condition

})->
			->whereHas('follower',function($query) user ($request){
			$query->where();//wuery condition

})->
                ->get();

1 like
anilkumarthakur60's avatar

@FounderStartup

$allusers = User::where('id', '<>', Auth::id() )
                ->where('status' , 1)
                ->whereRoleIs('broker')
                ->where('email_verified_at', '<>', NULL)
                ->where('mailforfollowers', 1)
				->whereHas('broker',function($query) user ($request){
			$query->where('name','LIKE','%'.$request->search.'%')

})->
			->whereHas('follower',function($query) user ($request){
			$query->where('name',$request->search)

})->
                ->get();
Snapey's avatar

errr.

$followers =  Auth::user()->followers()->get()
1 like
Snapey's avatar

@FounderStartup what? Relations 101

get the followers of the authenticated user. Not sure what else I can say without repeating the laravel docs

1 like
FounderStartup's avatar

@Snapey I did as follows :

   $allusers = Follower::whereHas('broker', function ($query) {$query
                    ->where('status', 1)
                    ->whereRoleIs('broker')
                    ->where('email_verified_at', '<>', NULL)
                    ->where('mailforfollowers', 1);
                })->get();

Is it the best way to solve it ?

kokoshneta's avatar

@FounderStartup What’s there to explain? Auth::user() gets the currently authenticated user; ->followers() returns the hasMany query created by the User::followers() method; ->get() retrieves the records from the database. You could do it even shorter and just use $followers = Auth::user()->followers, that does the same.

I think a few aspects of your code need more explaining, really:

  • Why does your Follower class have a follower() method? A follower has both a brokerid and a followerid (in addition to its own id), of which brokerid is the user that they follow (as defined in the followers() relationship in your user model) – so what is followerid? Can each follower in turn have just one specific follower?
  • Why do you call your user variable $allusers when you start out by filtering on the currently authenticated user’s ID, and thus, by definition, limiting the result set to one?

Your query is searching for any users (in practice, never more than one) whose status is 1, whose role is broker, whose email is verified, whose mailforfollowers field is 1, and whose broker() and follower() relationship both have records. That seems a little excessive at first glance. Will there ever be authenticated users who don’t match these criteria?

[Also, you have a syntax error: it should be whereHas('xyz', function($query) **USE** ($request), not user ($request).]

kokoshneta's avatar

@FounderStartup Okay, that code makes a little more sense – at least now you’re fetching a collection of users, not just a single user.

But now you’re not relating the followers to the authenticated user at all, just fetching all followers who have any broker whose status is 1, role is broker, email is verified and mailforfollowers is 1.

I think you need to clarify exactly what you want to do, and what attributes you expect your authenticated user to have.

1 like
Snapey's avatar
Snapey
Best Answer
Level 122

also have a look at scopes. where('status',1) tells anyone new to the code ( or future you) nothing about what this means

you could convert this to a scope like

->activeSubscriber()

or whatever status 1 actually means ( also use enums or constants instead of magic numbers)

same for

->verifiedEmail()

instead of the existing ->where('email_verified_at', '<>', NULL)

Please or to participate in this conversation.