Reputation: 37
I am developing a records where multiple tables record will display in a single view with collection of array. Ref from merge multiple collection in a single array
I am seeking help for :
Note : I am not using any relationship method to join these tables because none of these has primary common field.
There is an another way to solve this issue by create a common transaction table and all tables transaction record master data will save there. From that transaction table display records in a blade file by date wise. But it will create a huge data in that transaction table after certain period of time. And it is not a good process I thought.
My Controller
public function daybook_collection()
{
$vendor_id = Auth::User()->id;
$data['vendor_profile'] = Vendor_profile::with('vendor_basic')->where('vendor_id', $vendor_id)->first();
$vendor_sale = Vendorproduct_trn_sale::where('vendor_id', $vendor_id)->get();
$vendor_salereturn = Vendorproduct_trn_salereturn::where('vendor_id', $vendor_id)->get();
$vendor_receipt = Vendor_receipt::where('vendor_id', $vendor_id)->get();
$vendor_payment = Vendor_payment::where('vendor_id', $vendor_id)->get();
$vendor_purchase = Vendorproduct_trn_purchase::where('vendor_id', $vendor_id)->get();
$vendor_purchasereturn = Vendorproduct_trn_purchasereturn::where('vendor_id', $vendor_id)->get();
$vendor_debitnote = Vendor_debitnote::where('vendor_id', $vendor_id)->get();
$vendor_creditnote = Vendor_creditnote::where('vendor_id', $vendor_id)->get();
$data['daybookcollection'] = collect($vendor_sale)->merge($vendor_salereturn ?: collect())->merge($vendor_receipt ?: collect())->merge($vendor_payment ?: collect())->merge($vendor_purchase ?: collect())->merge($vendor_purchasereturn ?: collect())->merge($vendor_debitnote ?: collect())->merge($vendor_creditnote ?: collect())->sortBy('transaction_date');
//dd($data);
return view('vendor.accountreport.daybook', $data);
}
Array return like this
Expected output in blade file
Upvotes: 0
Views: 1139
Reputation: 134
To do this, while iterating over the $data['daybookcollection']
Collection, you will need to explain to php how to resolve that specific line through the class of the element we're iterating over. we can use get_class()
to get that information, in order to tie this all up, add a new array to your controller that maps each possible class with a bill type and id prefix :
public function daybook_collection()
{
//...
$bill_map = [
Vendorproduct_trn_sale::class => [
'type' => 'Sale',
'prefix' => 'S'
],
Vendor_receipt::class => [
'type' => 'Receipt',
'prefix' => 'R'
],
Vendor_payment::class => => [
'type' => 'Payment',
'prefix' => 'Py'
] //... Add all the classes you merged
];
return view('vendor.accountreport.daybook', compact('data', 'bill_map'));
}
So that inside your blade component, things will look like this :
@foreach($data['daybookcollection'] as $row)
<tr>
<td>{{ $row->created_at }}</td> //Date Column
<td>{{ $bill_map[get_class($row)]['type'] }}</td> //Bill Type Column
<td>{{ $bill_map[get_class($row)]['prefix'] . $row->id }}</td> //Bill Id Column
<td> {{ $row->amount }}</td>
</tr>
@endforeach
Warning: This solution assumes the created_at and amount fields exist in all the models you merged, otherwise for example if some models use an amount field and others use a price field you need to add the column of each class into the bill_map
Side-note : Having to resort to collection merges like this is not very clean, i would suggest you create a new abstraction layer, a model named 'Bill' for example, with a polymorphic relationship to all of these types of tables (Vendorproduct_trn_sale, Vendor_receipt..) that gets created and attached everytime an instance of these classes is created, this will make fetching the data for tables like these much easier
Upvotes: 1