I'm trying to pass a query result into a view, but I keep getting an error: Maximum execution time of 30 seconds exceeded I'm passing smaller objects through and its working fine, but it breaks when I pass the result of $monthlyBidStatment through. If there is a better way of doing this I'd really appreciate any advice. Thanks in advance.
public function sendEmailInvoice(Request $request)
{
$invoice = Invoice::where('id', '=', $request->invoiceId)->first();
$startDate = $invoice->start_billing_date;
$startDateSql = date("Y-m-d", strtotime($startDate));
$endDate = $invoice->end_billing_date;
$endDateSql = date("Y-m-d", strtotime($endDate));
$affiliate = AffiliateDetail::where('id', '=', $invoice->affiliate_detail_id)->first();
$monthlyBidStatment = BidTracker::where('affiliate_detail_id', '=', $affiliate->id)->whereBetween('created_at', [$startDateSql, $endDateSql])->get();
// LOG::info(gettype($monthlyBidStatment));
Mail::send('pdf.invoicePDF', [
'oAffiliate' => $affiliate,
'oInvoice' => $invoice,
'bids' => $monthlyBidStatment,
'showBreadcrumb' => false
], function ($m) use ($affiliate, $invoice, $monthlyBidStatment)
{
$m->from(env('APP_EMAIL'), env('APP_NAME'));
$m->to($affiliate->info_billing_email, $affiliate->info_company_name)->subject(env('APP_NAME') . ' Invoice');
$pdf = PDF::loadView('pdf.invoicePDF', [
'oAffiliate' => $affiliate,
'oInvoice' => $invoice,
'bids' => $monthlyBidStatment,
'showBreadcrumb' => false
]);
$m->attachData($pdf->output(), 'invoice.pdf');
});
$request->session()->flash('message', 'Successfully sent email invoice');
return array(
'status' => 'ok'
);
}
Check the following line:
$invoice = Invoice::where('id', '=', $request->invoiceId)->first();
// Its a collection object.
All multi-result sets returned by Eloquent are instances of the Illuminate\Database\Eloquent\Collection object, including results retrieved via the get method or accessed via a relationship. The Eloquent collection object extends the Laravel base collection, so it naturally inherits dozens of methods used to fluently work with the underlying array of Eloquent models.
In short terms we can say a collection object contains so many things in it, but in your case you only want the data part. So instead of passing the entire collection object, convert it into an array by iterating it with the required column and pass that array to email template. It will remove the overheads.
Ex:
$data = array();
$users = App\User::where('active', 1)->get();
foreach ($users as $user) {
$data[] = $user->name;
}
// Use $data where required