Я работаю над небольшим проектом, используя Laravel, сейчас я отображаю мои продукты, которые у меня есть в моей базе данных, в таблице HTML.
Итак, сейчас я создал еще одну таблицу базы данных под названием варианты для хранения продуктов variants
,
Я бы хотел count the number of variants
что у меня есть для each product
это мой код: как вы можете видеть, я также выбираю categories, brands, suppliers
я хочу посчитать число variants
(поскольку у меня уже есть таблица с именем variants
, содержит поле product_id
)
public function index()
{
$products = Product::with(['category', 'brand', 'supplier'])->orderBy('id', 'desc')->paginate(15);
return response()->json($products);
}
Мой текущий результат:
{
"current_page": 1,
"data": [
{
"id": 3,
"product_name": "Ipad",
"product_description": null,
"product_category": 4,
"product_supplier": 1,
"product_buying_price": "5500.00",
"product_selling_price": "66.00",
"product_min_stock": "10.00",
"product_max_stock": "10.00",
"product_alert_stock": 10,
"product_alert_phone": "+1 (1084) 565-43109",
"product_alert_email": "daqaxipig@mailinator.net",
"product_cost_price": "65.00",
"product_supplier_discount": "10.00",
"product_brand": 5,
"product_warehouse": 2,
"product_availability": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"category": {
"id": 4,
"category_name": "TABLET",
"category_parent": null,
"category_description": null,
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"brand": {
"id": 5,
"brand_name": "Apple",
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"supplier": {
"id": 1,
"supplier_company": "Dataxpress",
"supplier_code": "55598548455",
"supplier_tax_number": "68958955",
"supplier_phone": "4382251059",
"supplier_fax": "4382251059",
"supplier_website": "oppo.com",
"supplier_email": "ab@dothostia.com",
"supplier_note": "Aliquip porro est n",
"supplier_type": 1,
"supplier_address": "Consequuntur maxime",
"supplier_city": "Qui consectetur non",
"supplier_state": "Ex nemo sit quia ame",
"supplier_zip": "46033",
"supplier_country": "HU",
"supplier_representative": 2,
"supplier_price_list": 4,
"supplier_tax_type": 2,
"supplier_currency": "GBP",
"deleted_at": null,
"created_at": "2020-01-17 18:16:55",
"updated_at": "2020-01-17 18:16:55"
}
},
{
"id": 2,
"product_name": "Samsung A35",
"product_description": "test",
"product_category": 1,
"product_supplier": 1,
"product_buying_price": "500.00",
"product_selling_price": "500.00",
"product_min_stock": "10.00",
"product_max_stock": "20.00",
"product_alert_stock": 15,
"product_alert_phone": "+1 (1023) 803-2526",
"product_alert_email": "duvupihujo@mailinator.net",
"product_cost_price": "600.00",
"product_supplier_discount": "30.00",
"product_brand": 4,
"product_warehouse": 2,
"product_availability": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"category": {
"id": 1,
"category_name": "Super Phone",
"category_parent": null,
"category_description": null,
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"brand": {
"id": 4,
"brand_name": "Samsung",
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"supplier": {
"id": 1,
"supplier_company": "Dataxpress",
"supplier_code": "55598548455",
"supplier_tax_number": "68958955",
"supplier_phone": "4382251059",
"supplier_fax": "4382251059",
"supplier_website": "oppo.com",
"supplier_email": "ab@dothostia.com",
"supplier_note": "Aliquip porro est n",
"supplier_type": 1,
"supplier_address": "Consequuntur maxime",
"supplier_city": "Qui consectetur non",
"supplier_state": "Ex nemo sit quia ame",
"supplier_zip": "46033",
"supplier_country": "HU",
"supplier_representative": 2,
"supplier_price_list": 4,
"supplier_tax_type": 2,
"supplier_currency": "GBP",
"deleted_at": null,
"created_at": "2020-01-17 18:16:55",
"updated_at": "2020-01-17 18:16:55"
}
},
{
"id": 1,
"product_name": "Elton Mullins",
"product_description": "Possimus rerum comm",
"product_category": 2,
"product_supplier": 1,
"product_buying_price": "97.00",
"product_selling_price": "354.00",
"product_min_stock": "10.00",
"product_max_stock": "20.00",
"product_alert_stock": 5,
"product_alert_phone": "+1 (884) 587-2414",
"product_alert_email": "pywu@mailinator.com",
"product_cost_price": "989.00",
"product_supplier_discount": "10.00",
"product_brand": 3,
"product_warehouse": 2,
"product_availability": 2,
"deleted_at": null,
"created_at": null,
"updated_at": null,
"category": {
"id": 2,
"category_name": "Phones",
"category_parent": null,
"category_description": null,
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"brand": {
"id": 3,
"brand_name": "Oppo",
"deleted_at": null,
"created_at": null,
"updated_at": null
},
"supplier": {
"id": 1,
"supplier_company": "Dataxpress",
"supplier_code": "55598548455",
"supplier_tax_number": "68958955",
"supplier_phone": "4382251059",
"supplier_fax": "4382251059",
"supplier_website": "oppo.com",
"supplier_email": "ab@dothostia.com",
"supplier_note": "Aliquip porro est n",
"supplier_type": 1,
"supplier_address": "Consequuntur maxime",
"supplier_city": "Qui consectetur non",
"supplier_state": "Ex nemo sit quia ame",
"supplier_zip": "46033",
"supplier_country": "HU",
"supplier_representative": 2,
"supplier_price_list": 4,
"supplier_tax_type": 2,
"supplier_currency": "GBP",
"deleted_at": null,
"created_at": "2020-01-17 18:16:55",
"updated_at": "2020-01-17 18:16:55"
}
}
],
"first_page_url": "http://app.quicknsales.com/backend/product?page=1",
"from": 1,
"last_page": 1,
"last_page_url": "http://app.quicknsales.com/backend/product?page=1",
"next_page_url": null,
"path": "http://app.quicknsales.com/backend/product",
"per_page": 15,
"prev_page_url": null,
"to": 3,
"total": 3
}
мне нужно добавить ключ для ex: nbr_variants: x, элемент данных foreach