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

lijith's avatar

Get all details from three tables

I have 3 tables User, Profile, Subscription

subscribers table has a field to flag active/inactive state

Relation are set as User

public function profile(){
    return $this->hasOne('App\Profile');
  }

  public function subscription(){
    return $this->hasOne('App\Subscriber');
  }

Profile

public function user(){
    return $this->belongsTo('App\User');
  }

Susbcriber

public function subscriber() {
        return $this->belongsTo('App\User');
    }

I want to get the details of active subscribers with

Subscriber::with('subscriber')
            ->where('active', '=', '1')
            ->where('digital', '=', '1')
            ->get();

it only has details from User and Subscriber table, how do i also connect with Profile table also?

0 likes
10 replies
bobbybouwmann's avatar

You can keep chaining here

$subcribers = Subscriber::with('subscriber.profile')
            ->where('active', '=', '1')
            ->where('digital', '=', '1')
            ->get();

Now you can access the profile as well

foreach($subcribers as $subscriber) {
    echo $subscriber->subscriber->profile->name;
}
lijith's avatar

@bobbybouwmann Thank you. I was just looking at eager loading. is it possible to do it like user with profile, subscription(where active=1 and digital=1)

bobbybouwmann's avatar

You mean you go the other way? From user to subscriber?

User::with(['profile', 'profile.subcribers' => function($query) {
    $query->where('active', '=' ,1)
        ->where('digital', '=', 1)
})]->get();
lijith's avatar

yes. but i got this. Call to undefined method Illuminate\Database\Query\Builder::subcribers()

can you explain profile.subcribers ?

bobbybouwmann's avatar

My mistake! It's subscription instead of subscriber!

User::with(['profile', 'subscription' => function($query) {
    $query->where('active', '=' ,1)
        ->where('digital', '=', 1)
})]->get();

I thought the relation was setup different, but the dot notation means you can chain into relations

Example: http://laravel-tricks.com/tricks/chaining-eager-loading

lijith's avatar

thanks for explaining. but this setup pulls every rows from user table with its relation. condition is not checked.

lijith's avatar

i am trying to understand how this works. can you explain why this pulled all records without meeting condition

User::with(['profile', 'subscription' => function($query) {
    $query->where('active', '=' ,1)
        ->where('digital', '=', 1)
})]->get();
bobbybouwmann's avatar

Well it pulls all users with their profile and their subscription. Only the subscriptions are sorted right now! If you only want to users with that you can use whereHas

$usersWithDigitalSubscription = User::whereHas('subscription', function($query) {
    $query->where('active', '=' ,1)->where('digital', '=', 1);
})->get();
lijith's avatar
User::with(['profile', 'subscription' => function($query) {
    $query->where('active', '=' ,1)
        ->where('digital', '=', 1)
})]->get();

this will work if the profile and subscription are related? if no then when will the condition kicks in

Please or to participate in this conversation.