Passing a collection to a mailable| Laravel 5.4 - laravel

I am trying to get a mailable setup which has a collection of files. Mail controller looks like:
<?php
namespace App\Mail;
use App\Document;
use App\Order;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
use App\User;
class OrderComplete extends Mailable
{
use Queueable, SerializesModels;
public $user;
public $order;
public $documents;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $user, Order $order, Document $document)
{
//
$this->user = $user;
$this->order = $order;
$this->documents = $document;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('emails.customers.complete');
}
}
Controller calling the mailable looks like;
use App\Document;
// other code...
$documents = Document::where('order_id', $orderId)
->where('product', 'like', '%response')
->get();
Mail::to($customer)
->send(new OrderComplete($customer, $order, $documents));
But I keep getting this error:
Type error: Argument 3 passed to App\Mail\OrderComplete::__construct() must be an instance of App\Document, instance of Illuminate\Database\Eloquent\Collection given, called in /Users/ap/sites/propair/app/Http/Controllers/OrderController.php on line 253
I'm pretty confused as I thought this should work?
thanks

This function declaration:
public function __construct(..., Document $document)
means PHP will enforce that $document is an instance of App\Document.
If you want to pass it a collection instead, you'll need to do:
public function __construct(..., \Illuminate\Database\Eloquent\Collection $documents)

Related

How to passing variable from controller to view queued mail?

I'm trying to pass variable $array from controller to mail blade, but whenever I run queue:listen. It always say failed.
Bellow is my code
In controller I have a variable named $array, I've putting it in dispatch
Controller
$array["view"] = "layouts.mail.order";
$array["subject"] = "Order Created";
$array["from"] = env('MAIL_USERNAME');
$array["data"] = "aaaaaaaaa";
$array["email"] = Auth::user()->email;
OrderEmailJob::dispatch($array);
OrderEmailJob
<?php
namespace App\Jobs;
use App\Mail\OrderMail;
use Illuminate\Bus\Queueable;
use Illuminate\Support\Facades\Mail;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
class OrderEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
public $array;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($array)
{
$this->array = $array;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new OrderMail();
Mail::to($this->array['email'])->send($array);
}
}
and this is code for the mailable
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class OrderMail extends Mailable
{
use Queueable, SerializesModels;
public $array;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($array)
{
$this->array = $array;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view($this->array['view'])
->from($this->array['from'], env('MAIL_FROM_NAME'))
->subject($this->array['subject'])
->with([
'data' => $this->array['data'],
]);
}
}
The result I want is I can use variable $array in view for my mail, because I've to printed out data from $array variable
Sorry about my english, thanks
try like this :
public $mailData;
public function __construct($mailData)
{
$this->mailData = $mailData;
}
public function build()
{
// Array for Blade
$input = array(
'action' => $this->mailData['action'],
'object' => $this->mailData['object'],
);
return $this->view('emails.notification')
->with([
'inputs' => $input,
]);
}
I'm not sure, the answer correct. But you can change the name variable $array to $data and check again. Maybe your variable name is a special case like array keyword

Laravel 7 queued email very slow with attachment but fast without

I use Laravel 7 queues / jobs to send a newsletter to multiple addresses and it works well and rather fast. But when I send a single email with attachment (22ko PDF), it takes nearly 3 - 5 minutes to get through. Any clues? I use database driver and Mailgun API.
How can I see if the slow process time is from Laravel app or Mailgun or else?
I tried to use SendEmailJob::dispatchNow($data); but it does not speed up the process.
app\Http\controllers/EmailController.php
App\EmailController
use App\Job\SendEmailJob;
public function send()
{
$data = array(
'from_name'=>from_name',
'from_email'=>'admin#domain.com',
'to_name'=>$user->firstname." ".$user->name,
'to_email'=>$user->email,
'subject'=>'This is test email',
'reply_to'=>noreply#domain.com,
'model'=>$invoice,
'locale'=>app()->getLocale(),
'user'=>$user,
'view'=>'emails.invoice',
'filefullpath'=>$fullfilepath,
);
SendEmailJob::dispatch($data);
}
App\Jobs\SendEmailJob.php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Mail\SendView;
use Mail;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $data;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new SendView($this->data);
$email->replyTo($this->data['reply_to']);
$email->subject($this->data['subject']);
if(isset($this->data['filefullpath']))
{
$email->attach($this->data['filefullpath']);
}
Mail::to($this->data['to_email'], $this->data['to_name'])->send($email);
}
}
App\Mail\SendView.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class SendView extends Mailable
{
use Queueable, SerializesModels;
protected $data;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($data)
{
$this->data = $data;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
$view = $this->data['view'];
$user = $this->data['user'];
$model = $this->data['model'];
$locale = $this->data['locale'];
$array = $this->data['array'];
return $this->view($view, compact('user', 'model','locale','array'));
}
}
Please help! Thanks.

Laravel queues issues when I pass data from controller to jobs

I'm having issue fixing the queue part for Laravel.
I have a contact form which works perfectly. I submit the form and a mail is being sent to me with the input details from the form.
But the issue is that if you do not use the Laravel Queues then the UX is lacking because of the waiting time before the mail is being sent and after getting the success message.
So now I want to implement the queuing part, but im not doing it right. Im still a noob at it...
Here is my controller
KontaktformController.php
<?php
namespace App\Http\Controllers;
use App\Jobs\SendEmailJob;
use Carbon\Carbon;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Session;
use App\Kontaktform;
use Illuminate\Http\Request;
use App\Mail\SendEmailMailable;
class KontaktformController extends Controller
{
public function create()
{
return view('kontakt');
}
public function store(Request $request) {
$this->validate($request, [
'name' => 'required|string',
'mobile' => 'required',
'email' => 'required|email',
'subject' => 'required',
'message' => 'required',
]);
/*
Add mail functionality here.*/
$kontakt = new Kontaktform([
'navn' => $request['name'],
'mobilnr' => $request['mobile'],
'fastnetnr' => $request['landline'],
'mail' => $request['email'],
'emne' => $request['subject'],
'beskrivelse' => $request['message']
]);
$kontakt->save();
//$user['email'] = 'test#test.com';
$job = (new SendEmailJob($request['email']))
->delay(Carbon::now()->addSeconds(3));
dispatch($job);
}
}
SendEmailJob.php
<?php
namespace App\Jobs;
use App\Mail\SendEmailMailable;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Support\Facades\Mail;
class SendEmailJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $user;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$email = new SendEmailMailable('test#test.dk');
Mail::to('admin#admin.dk')->send($email);
}
}
SendEmailMailable.php
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SendEmailMailable extends Mailable
{
use Queueable, SerializesModels;
public $request;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('test#test.dk')->view('mail.kontaktform');
}
}
kontaktform.blad.php
{{$request['email']}}
In your kontaktform.blad.php (which should be named kontaktform.blade.php btw), you expect the data to be an array because you try to fetch the index ['email'] though in your SendEmailJob.php in the method handle() you pass your email as a string.
So what you should change it to the following:
$email = new SendEmailMailable(['email' => 'test#test.dk']);
or do the following in the controller & in the job:
Controller:
$job = (new SendEmailJob($request))
->delay(Carbon::now()->addSeconds(3));
Job:
protected $request;
/**
* Create a new job instance.
*
* #return void
*/
public function __construct($request)
{
$this->request = $request;
}
/**
* Execute the job.
*
* #return void
*/
public function handle()
{
$mailable = new SendEmailMailable($this->request);
Mail::to('admin#admin.dk')->send($mailable);
}
Now all data from the request is should be available in your blade template.
I found the issue. I didnt pass data from mailable to my blade
public function build()
{
return $this->view( 'mail.kontaktform' )->with( [
'request' => $this->request,
] );
}

Laravel pass object to view

When I'm trying to pass an object through a view with the AppServiceProvider it gives an error an says
Trying to get property of non-object
This is currently my App\Providers:
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use App\User;
use Auth;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
$users = Auth::user();
view()->share('user','users');
}
/**
* Register any application services.
*
* #return void
*/
public function register()
{
//
}
}
And when I say in the view:
{!! user->name !!}
It throws an error.
you are missing the '$' on users when passing the variable to view. So it can't be rendered on the view.
it must be:
$users = Auth::user();
view()->share('user', $users);

Laravel5 default pagination

I have an error when simply put this
->paginate(15);
what is wrong with my code below
<?php namespace App\Http\Controllers;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Order;
use Illuminate\Http\Request;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Pagination\Paginator as Paginator;
class OrderController extends Controller {
/**
* Create a new controller instance.
*
* #return void
*/
public function __construct()
{
$this->middleware('auth');
}
/**
* Display a listing of the resource.
*
* #return Response
*/
public function index()
{
$orders = Order::with('customer')->get()->paginate(5);
return view('order.index', compact('orders'));
}
}
appreciate if you someone can highlight to me what have I done wrong?
Remove the get() when you use paginate()
$orders = Order::with('customer')->paginate(15);

Resources