I'm having trouble to not show same data in the table, but want to sum
attributes' values to the old identical data's attributes. I have 6 Models, Purchase
, Inventory
, Slab
, Scarting
, floorTile
and MarbleType
. Purchase
and Inventory
has One to one relation. Then Inventory
further has one to one relation with Slab
, Scarting
and FloorTile
. I'm fetching data using query on Purchase
model. i.e.
$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": 58,
"marbleName_id": 2,
"fname": "Sama",
"lname": "Jojo",
"factoryName": "Sama Inc.",
"cnic": null,
"area": "Johar town",
"city": "Lahore",
"province": "Punjab",
"phone1": "45678912345",
"phone2": "45678912345",
"inventory_ID": 29,
"created_at": "2019-05-06 09:51:51",
"updated_at": "2019-05-06 09:51:51",
"inventory": {
"id": 29,
"purchase_id": 58,
"marbleType": "1",
"marbleName_id": 2,
"totalSquareFt": 20,
"priceperSquareFt": 20,
"totalPurchasePrice": 400,
"standardSize_ID": "4",
"salePrice": 25,
"miliMeter": "8mm",
"slab_ID": 10,
"scarting_ID": null,
"floorTile_ID": null,
"created_at": "2019-05-06 09:51:51",
"updated_at": "2019-05-06 09:51:51",
"slab": {
"id": 10,
"marbleName_ID": "2",
"inventory_ID": "29",
"created_at": "2019-05-06 09:51:51",
"updated_at": "2019-05-06 09:51:51"
}
}
},
{
"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
}
}
This query gets all data, and using foreach loop i'm just getting only Slab
s' data, Loop is as follows:
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 = [];
}
}
As you can at Slab
id '1' and '10' both have same marbleName_ID
. I'm showing this data on blade in the table format, but i want if specific MarbleName_ID
has been iterated through the loop once, and when it comes again in the future iteration then not to show it in the table but Add this Second marbleName_ID
's attributes' values (i.e. totalSquareFt
and totalPurchasePrice
) to previous same marbleName_ID
's attributes' values.
Here is the code of blade:
@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
Any help would be highly appreciated.