John Davidson

php - Display collection of arrays in blade file Laravel 8

0 comments
Message:


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 :



  1. How to display collection of array records under daybookcollection in a blade file within foreach loop (that stored in a single array.)

  2. How to show these records in date wise in ascending order ?


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




My Answer:
>vendor_id;
$sales = Sale::where('vendor_id', $vendor_id)->get();
$purchases = Purchase::where('vendor_id', $vendor_id)->get();
$expenses = Expense::where('vendor_id', $vendor_id)->get();

$daybookCollection = collect([$sales, $purchases, $expenses])->collapse();

$sortedDaybookCollection = $daybookCollection->sortBy('date');

return view('daybook_collection', compact('sortedDaybookCollection'));
}

Blade File@foreach($sortedDaybookCollection as $record)

Date: {{ $record->date }}


Amount: {{ $record->amount }}


Type: {{ get_class($record) }}


@endforeach

In this code snippet, we first retrieve the sales, purchases, and expenses records for the logged-in vendor. We then merge these collections into a single collection called $daybookCollection using the collapse() method.

Next, we sort the $daybookCollection by the 'date' attribute in ascending order and pass it to the blade file as $sortedDaybookCollection.

In the blade file, we loop through each record in $sortedDaybookCollection and display the date, amount, and type of the record. The get_class() function is used to get the class name of the record to determine its type (sales, purchases, or expenses).

Rate this post

3 of 5 based on 3441 votes

Comments




© 2024 Hayatsk.info - Personal Blogs Platform. All Rights Reserved.
Create blog  |  Privacy Policy  |  Terms & Conditions  |  Contact Us