Foreach循环在每次迭代中显示相同的数据

时间:2019-05-03 13:50:39

标签: laravel loops if-statement foreach

我有5个模特。 PurchaseInventorySlabScartingFloorTilePurchaseInventory具有一对一的关系,然后InventorySlabScartingFloorTile具有一对一的关系。

$purchase = Purchase::where('factoryName', $factoryName)->with('inventory.slab')->get();

以上查询返回的正是我想要的。

结果:

[
  {
    "id": 36,
    "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": 9,
        "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,
    "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"
            }
        }
    }
]

此后,我获取了marbleName_ID并从Marble表中获取该ID的数据。像这样

foreach ($purchase as $pur) {

            if ($pur->inventory->marbleType == 1) {
                $slabe = 'Slab';
            }

            if ($pur->inventory->slab_ID != NULL) {
                $marble_id = $pur->inventory->slab->marbleName_ID;
                $marble = MarbleType::where('id', $marble_id)->get();
            }
        }

此查询显示我想要的确切结果。 以上查询的结果如下:

[{"id":2,"marbleType":"Indian Marble","created_at":"2019-02-22 13:11:11","updated_at":"2019-02-22 13:11:11"}]
[{"id":3,"marbleType":"Russian Marble","created_at":"2019-02-22 13:16:12","updated_at":"2019-02-25 09:15:00"}] ```

But in blade its showing the data of only one id in each iteration

http://prntscr.com/njy1i5

The code of blade is following:
<tbody>
    @foreach($purchase  as $pur )
       @foreach($marble as $mar)
           <tr>
              <td>{{ $slabe }}</td>
              <td>{{ $mar->marbleType }}</td>
              <td>{{ $pur->inventory->totalSquareFt }}</td>
              <td>{{ $pur->inventory->totalPurchasePrice }}</td>
            </tr>
        @endforeach    
   @endforeach
</tbody>

1 个答案:

答案 0 :(得分:1)

这是因为您必须在购买的商品和大理石之间建立联系,然后才能执行此操作:

 @foreach($purchase  as $pur )
       @foreach($pur->marbles as $mar)
           <tr>
              <td>{{ $slabe }}</td>
              <td>{{ $mar->marbleType }}</td>
              <td>{{ $pur->inventory->totalSquareFt }}</td>
              <td>{{ $pur->inventory->totalPurchasePrice }}</td>
            </tr>
        @endforeach    
   @endforeach

我不建议这样做,但是使用您的代码将是这样:

foreach ($purchase as &$pur) {

            if ($pur->inventory->marbleType == 1) {
                $slabe = 'Slab';
            }

            if ($pur->inventory->slab_ID != NULL) {
                $marble_id = $pur->inventory->slab->marbleName_ID;
                $pur->marble = MarbleType::where('id', $marble_id)->get();
            }
        }

为了更好的代码易懂性,我建议在处理项目集合(例如purchaseS和marbleS)时使用复数形式