R
R
Roman2019-07-25 22:18:01
Laravel
Roman, 2019-07-25 22:18:01

Laravel with how to use where on data from intermediate table?

There are 4 tables.
account_service (pivot table):
user_id (Belongs To Many)
service_id (Belongs To Many)
status
services:
id
tariff_id (One to One) platform_id
(One to One)
platforms:
id
name
tariffs:
id
name
user:

$services = $this->with('services.tariff','services.platform')->first()->services;

I can refer to $services[0]->pivot->status;
Is it possible to make a selection in the query according to the condition of an adjacent table, where, for example, the status is 1, so as not to do a search in a loop.
Tried like this:
$services = $this->with('services.tariff','services.platform')->whereHas('services', function($q) {
            $q->where('status', '=', 1);
})->first()->services;

but unsuccessfully...

Answer the question

In order to leave comments, you need to log in

1 answer(s)
J
jazzus, 2019-07-25
@RomanTrakhtenberg

In the controller

$users = User::with(['services' => function($query) {
                  $query->with('tariff','platform')
           ->wherePivot('status',true);
    }])->get();

In model add to relations ->withPivot('status')

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question