У меня 5 моделей.Purchase
, Inventory
, Slab
, Scarting
и FloorTile
.Purchase
имеет отношение один к одному с Inventory
, а затем Inventory
имеет еще одно отношение к одному с Slab
, Scarting
и FloorTile
.
$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
.Как этот
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>