D
D
Danil Sysoev2020-04-24 11:25:41
Laravel
Danil Sysoev, 2020-04-24 11:25:41

How to make the right attitude?

I have such a task. For example: There are 2 tables. Users and e.g. phones

id|name|money
1|Kent|0
2|Tnek|100
3|Nect|100


id|user_id|model|buy
1| 1|Nokia|100
1| 1|Xiaom|150

Users 2 and 3 have money for a Nokia phone, but they haven't bought it yet. How to get a list of these users?

Users::where('money', '>=', 100)->hasMany('Mobiles');
I seem to be completely confused in three pines. Which relation to use, and which table to make a subordinate? Or is it impossible to get by with relationships, and will have to choose them in a cycle?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2020-04-24
@YoungOldMan

Then so

$users = User::doesntHave('phones')
             ->where('money', '>=', 100)
             ->get();

// в User
public function phones()
{
   return $this->hasMany('App\Models\Phone');
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question