V
V
Vavilov5732018-05-25 09:11:37
Laravel
Vavilov573, 2018-05-25 09:11:37

Order by retaliation field, laravel?

There is a product table, there is a city table.
There is a city_product. In each city, the price of goods may be different. I need to get a list of goods, and a list of cities in which it is. The default is to sort by product name, so I do $products = Product:with('cities').
But if the filter by city is selected, then you need to filter by price. How to do it better?
join ? But it's still a halo for me to use with to get the model of the city. Then do both with() and join ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
arab789, 2018-05-31
@arab789

Make a connection in the city model with city_products.
Then the condition, if there is a city, add the query:

$products = $products ->whereHas('city_product', function($q) use ($city){
                $q->whereIn('city', $city)->orderBy('price','desc');
            });

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question