У меня есть таблица продуктов, и у каждого продукта может быть много инвентаря.Результатом запроса, которого я хочу добиться, является сортировка продуктов по полю цены в таблице инвентаризации.
$products = Product::with(['inventories' => function($query){
$query->where('count', '>', 0)->orderBy('id');
}])->paginate(16);
return $products;
Результат:
data: [
{
id: 1,
brand_id: 3,
category_id: 5,
title: "فشارسنج عقربه ای و گوشی پزشکی هانسون مدل وکتو",
body: null,
sales: "2",
image: "images/product/product-image-index-5ca0b4607158f.jpg",
post_on: "15",
fast_shipping_cost: "50000",
weight: "300",
created_at: "2019-04-21 15:27:58",
updated_at: "2019-04-21 15:27:58",
deleted_at: null,
inventories: [
{
id: 1,
product_id: 1,
count: "10",
price: "75000",
expiration_date: null
}
]
},
{
id: 2,
brand_id: 3,
category_id: 5,
title: "فشارسنج دیجیتال مدیسانا مدل BU 550 Connect",
body: null,
sales: "10",
image: "images/product/product-image-index-5cb70c11cfaf0.jpg",
post_on: "20",
fast_shipping_cost: null,
weight: "250",
created_at: "2019-04-22 15:27:58",
updated_at: "2019-04-22 15:27:58",
deleted_at: null,
inventories: [
{
id: 2,
product_id: 2,
count: "5",
price: "150000",
expiration_date: null
},
{
id: 4,
product_id: 2,
count: "7",
price: "155000",
expiration_date: null
}
]
},
{
id: 3,
brand_id: 1,
category_id: 6,
title: "سوزن تست قند خون تی دی-تین بسته 100 عددی",
body: null,
sales: "5",
image: "images/product/product-image-index-5cbafe568518c.jpg",
post_on: null,
fast_shipping_cost: null,
weight: "150",
created_at: "2019-04-23 15:27:58",
updated_at: "2019-04-23 15:27:58",
deleted_at: null,
inventories: [
{
id: 3,
product_id: 3,
count: "4",
price: "98000",
expiration_date: null
}
]
}
],
Сортировка по $query->where('count', '>', 0)->sortBy('price')
Выполнить сортировку только вЗапасы, которые я хочу, чтобы этот вид применялся к продуктам.
Я хочу, чтобы результат был таким:
Сортировка по цене инвентаря, и в результате получается только один запас.
Сначала 150000, затем 98000, а затем 50000
data: [
{
id: 2,
brand_id: 3,
category_id: 5,
title: "فشارسنج دیجیتال مدیسانا مدل BU 550 Connect",
body: null,
sales: "10",
image: "images/product/product-image-index-5cb70c11cfaf0.jpg",
post_on: "20",
fast_shipping_cost: null,
weight: "250",
created_at: "2019-04-22 15:27:58",
updated_at: "2019-04-22 15:27:58",
deleted_at: null,
inventories: [
{
id: 2,
product_id: 2,
count: "5",
price: "150000",
expiration_date: null
},
]
},
{
id: 3,
brand_id: 1,
category_id: 6,
title: "سوزن تست قند خون تی دی-تین بسته 100 عددی",
body: null,
sales: "5",
image: "images/product/product-image-index-5cbafe568518c.jpg",
post_on: null,
fast_shipping_cost: null,
weight: "150",
created_at: "2019-04-23 15:27:58",
updated_at: "2019-04-23 15:27:58",
deleted_at: null,
inventories: [
{
id: 3,
product_id: 3,
count: "4",
price: "98000",
expiration_date: null
}
]
}
{
id: 1,
brand_id: 3,
category_id: 5,
title: "فشارسنج عقربه ای و گوشی پزشکی هانسون مدل وکتو",
body: null,
sales: "2",
image: "images/product/product-image-index-5ca0b4607158f.jpg",
post_on: "15",
fast_shipping_cost: "50000",
weight: "300",
created_at: "2019-04-21 15:27:58",
updated_at: "2019-04-21 15:27:58",
deleted_at: null,
inventories: [
{
id: 1,
product_id: 1,
count: "10",
price: "75000",
expiration_date: null
}
]
},
],