嵌套的foreach循环以显示数据

时间:2019-05-06 14:32:50

标签: laravel foreach

我有5个模型PurchaseInventorySlabScartingFloorTile。我从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()

有人可以指出我在做什么错吗??

1 个答案:

答案 0 :(得分:1)

您可能需要在else检查中使用$slab->inventory->slab_ID语句,否则,您将尝试循环null$slab->marble等于null )。如果您做了else { $slab->marble = []; },则可以使用该foreach而不必担心代码。

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();
  } else {
    $slab->marble = [];
  }

  foreach($slab->marble AS $mar){
    ...
  }
}

旁注,此代码可能效率很低。每当您需要循环运行查询时,都可能会遇到瓶颈。考虑使用一种关系,并希望在您的初始查询中加载它。

另外,如果$slab->inventorynull,则在使用trying to get property of non-object时会遇到$slab->inventory->slab_ID错误。