I have 5 Models. Purchase
, Inventory
, Slab
, Scarting
and FloorTile
. Purchase
has one to one relation with Inventory
and then Inventory
has further one to one relation with Slab
, Scarting
and FloorTile
.
$purchase = Purchase::where('factoryName', $factoryName)->with('inventory.slab')->get();
The above query is returning exactly what i want.
Result:
[
{
"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"
}
}
}
]
After that i've fetched marbleName_ID
and get data against that id from Marble
table. Like this
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();
}
}
This query is showing exact result what i want. The result of above query is as Follows:
[{"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></td>
<td></td>
<td></td>
<td></td>
</tr>
@endforeach
@endforeach
</tbody>
Aucun commentaire:
Enregistrer un commentaire