-1

I have a question about the dynamic relationship properties, suppose there are user and address model.

public function address(){
    return $this->hasMany(Address::class);
}

I want to ask $user->address will cause N+1 problems , therefore, we may use eager load, but when I call $user->address()->get() will also cause N+1 problems, is it a good performance?

Another question is that what is the better way to get the list of address with specific user?

I want to get only the address data without the user data, therefore, I dont want to use eager load.

dns_nx
  • 3,651
  • 4
  • 37
  • 66
yuk
  • 49
  • 8
  • what do you mean by `get only the address data without user data`, when you already user data in `$user`. – shyammakwana.me Mar 15 '21 at 10:57
  • This is because if i use eager load , i will get the data of user and the address data. Therefore, i want to know how to get only the addresss like $user->address()->get() .But $user->address()->get() will run many queries when number of address increase. – yuk Mar 15 '21 at 10:58
  • If you want to get the address without the user, then you have to load addresses in the controller instead of loading the user. Therefore you can create a separate function in your Repository. But for a real solution we need to see more code... – dns_nx Mar 15 '21 at 11:00
  • Then you should write a simple query to fetch the addresses using user_id. – MAY Mar 15 '21 at 11:00

3 Answers3

1

if you only want list of user addresses without user data then simply do this:

Address::where('user_id', $user->id)->get();
MAY
  • 667
  • 1
  • 6
  • 21
1

You can get the addresses without fetching the user data (still need the user id)

Here an example no matter the type of relation.

$userId = 1;
$adresses = Address::whereHas('user', function($user) use ($userId) {
    $user->where('id', '=', $userId);
})->get();

If the relation is known to be one to many: Use @May Example

N69S
  • 16,110
  • 3
  • 22
  • 36
  • So what is the purpose of dynamic relationship property with hasMany relationship? – yuk Mar 15 '21 at 15:33
  • @yuk explain it more plainly. what do you mean by "dynamic relationship property". Are you asking what is the difference between `$user->address` and `$user->address()->get()` ? – N69S Mar 15 '21 at 18:41
-1

I find my answer is that

User::with('address')->find($id)->address 

I think it is a better answer get the address list of a user.

yuk
  • 49
  • 8
  • there is no difference between `$user->address` or `User::with('address')->find($id)->address ` except that in the second one, you lose the user object that you query – N69S Mar 15 '21 at 18:43
  • i mean the performance . the performane of this one is much better then the before because of eager load. it handle the n+1 problems. – yuk Mar 16 '21 at 00:27
  • If it's the performance you're after, then take @MAY solution or mine, both of them do run only one query. Your solution runs 2 queries, one for the user and another for the adresses – N69S Mar 16 '21 at 08:25