У меня есть 5 моделей Purchase
, Inventory
, Slab
, Scarting
и FloorTile
. Я получаю коллекцию из Purchase
модели, затем из этой коллекции я извлекаю данные из Inventory
таблицы. Затем, в зависимости от этих данных, я собираюсь продолжить модель Slab
, которая имеет отношение к Inventory
. Теперь, после получения данных из модели Slab
, я хочу показать это на блейде, используя цикл Foreach
. Но я получаю ошибку:
В foreach указан неверный аргумент ()
Я пробовал это в моем контроллере:
$slabs = Purchase::where('factoryName', $factoryName)->with('inventory.slab')->get();
Этот результат запроса выглядит следующим образом:
{
"id": 36,
"marbleType_id": 2,
"fname": "Sama",
"lname": "Jojo",
"factoryName": "Sama Inc.",
"cnic": "3216542",
"area": "Johar town",
"city": "Lahore",
"province": "Punjab",
"phone1": "45678912345",
"phone2": "45678912345",
"inventory_ID": 10,
"created_at": "2019-03-19 12:11:45",
"updated_at": "2019-03-19 12:11:45",
"inventory": {
"id": 10,
"purchase_id": 36,
"marbleType": "1",
"totalSquareFt": 25,
"priceperSquareFt": 230,
"totalPurchasePrice": 25000,
"standardSize_ID": "5",
"salePrice": 250,
"miliMeter": "6mm",
"slab_ID": 1,
"scarting_ID": null,
"floorTile_ID": null,
"created_at": "2019-03-19 12:11:45",
"updated_at": "2019-03-19 12:11:45",
"slab": {
"id": 1,
"marbleName_ID": "2",
"inventory_ID": "10",
"created_at": "2019-03-19 12:11:45",
"updated_at": "2019-03-19 12:11:45"
}
}
},
{
"id": 55,
"marbleType_id": 3,
"fname": "Sama",
"lname": "Jojo",
"factoryName": "Sama Inc.",
"cnic": "32165421",
"area": "Johar town",
"city": "Lahore",
"province": "Punjab",
"phone1": "45678912345",
"phone2": "45678912345",
"inventory_ID": 26,
"created_at": "2019-04-25 10:47:41",
"updated_at": "2019-04-25 10:47:41",
"inventory": {
"id": 26,
"purchase_id": 55,
"marbleType": "1",
"totalSquareFt": 12,
"priceperSquareFt": 12,
"totalPurchasePrice": 120,
"standardSize_ID": "1",
"salePrice": 15,
"miliMeter": "6mm",
"slab_ID": 9,
"scarting_ID": null,
"floorTile_ID": null,
"created_at": "2019-04-25 10:47:41",
"updated_at": "2019-04-25 10:47:41",
"slab": {
"id": 9,
"marbleName_ID": "3",
"inventory_ID": "26",
"created_at": "2019-04-25 10:47:41",
"updated_at": "2019-04-25 10:47:41"
}
}
},
{
"id": 56,
"marbleType_id": 4,
"fname": "Sama",
"lname": "Jojo",
"factoryName": "Sama Inc.",
"cnic": null,
"area": "Johar town",
"city": "Lahore",
"province": "Punjab",
"phone1": "45678912345",
"phone2": "45678912345",
"inventory_ID": 27,
"created_at": "2019-05-05 20:11:52",
"updated_at": "2019-05-05 20:11:52",
"inventory": {
"id": 27,
"purchase_id": 56,
"marbleType": "3",
"totalSquareFt": 20,
"priceperSquareFt": 20,
"totalPurchasePrice": 420,
"standardSize_ID": "3",
"salePrice": 25,
"miliMeter": "6mm",
"slab_ID": null,
"scarting_ID": null,
"floorTile_ID": 10,
"created_at": "2019-05-05 20:11:52",
"updated_at": "2019-05-05 20:11:52",
"slab": null
}
},
{
"id": 57,
"marbleType_id": 9,
"fname": "Sama",
"lname": "Jojo",
"factoryName": "Sama Inc.",
"cnic": null,
"area": "Johar town",
"city": "Lahore",
"province": "Punjab",
"phone1": "45678912345",
"phone2": "45678912345",
"inventory_ID": 28,
"created_at": "2019-05-05 20:32:41",
"updated_at": "2019-05-05 20:32:41",
"inventory": {
"id": 28,
"purchase_id": 57,
"marbleType": "2",
"totalSquareFt": 31,
"priceperSquareFt": 12,
"totalPurchasePrice": 400,
"standardSize_ID": "1",
"salePrice": 15,
"miliMeter": "8mm",
"slab_ID": null,
"scarting_ID": 4,
"floorTile_ID": null,
"created_at": "2019-05-05 20:32:41",
"updated_at": "2019-05-05 20:32:42",
"slab": null
}
},
{
"id": 59,
"marbleType_id": 12,
"fname": "Sama",
"lname": "Jojo",
"factoryName": "Sama Inc.",
"cnic": null,
"area": "Johar town",
"city": "Lahore",
"province": "Punjab",
"phone1": "12345678945",
"phone2": "45678945645",
"inventory_ID": 30,
"created_at": "2019-05-06 10:14:35",
"updated_at": "2019-05-06 10:14:35",
"inventory": {
"id": 30,
"purchase_id": 59,
"marbleType": "2",
"totalSquareFt": 12,
"priceperSquareFt": 12,
"totalPurchasePrice": 122,
"standardSize_ID": "1",
"salePrice": 12,
"miliMeter": "8mm",
"slab_ID": null,
"scarting_ID": 5,
"floorTile_ID": null,
"created_at": "2019-05-06 10:14:35",
"updated_at": "2019-05-06 10:14:35",
"slab": null
}
}
Вот цикл Foreach в контроллере для получения данных, относящихся только к Slab
.
foreach ($slabs as $slab) {
if ($slab->inventory->slab_ID != NULL) {
$marble_id = $slab->inventory->slab->marbleName_ID;
$slab->marble = MarbleType::where('id', $marble_id)->get();
}
}
А вот содержимое блейда, где я хочу показать данные Slab
в таблице.
@foreach($slabs as $slab)
@foreach($slab->marble as $mar)
<tr>
@if($slab->inventory->marbleType == 1)
<td> Slab </td>
@endif
<td>{{ $mar->marbleType }}</td>
<td>{{ $slab->inventory->totalSquareFt }}</td>
<td>{{ $slab->inventory->totalPurchasePrice }}</td>
<td>{{ date('M j, Y h:ia' , strtotime( $slab->created_at )) }}</td>
</tr>
@endforeach
@endforeach
Это ошибка, которую я получаю в блейд-файле:
Invalid argument supplied for foreach()
Может кто-нибудь указать, что я делаю не так ??