lundi 9 décembre 2019

How to pass a collection to a Laravel Mailable class

I have googled around for similar questions but can't find a solution. I have a collection of orders returned in my Laravel controller like so;

$user = Auth::user();    
$items = Order::where('status', 'complete')->get();

I need to pass this collection along with the $user object to my OrderStatus Mailable class so i did this;

Mail::to($user->email)->send(new OrderStatus($user, $items));

In my OrderStatus mailable class;

class OrderStatus extends Mailable
{
    use Queueable, SerializesModels;

    public $user;
    public $items;

    public function __construct(User $user, Order $items)
    {
        $this->user = $user;
        $this->items = $items;
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        return $this->view('emails.order_status');
    }
}

Inside my order_status.blade.php file, i try to iterate through the collection. I get this error in my network tab when i try to send the mail;

Argument 2 passed to App\Mail\OrderStatus::__construct() must be an instance of App\Order, instance of Illuminate\Database\Eloquent\Collection given, called in C:\wamp64\www\VanityFlair\app\Http\Controllers\OrderController.php on line 131"

When i remove the $items collections, the mail sends and everything is fine, obviously meaning the collection is the issue. What is the correct way to pass collections to a mailable class and then to the view blade file in laravel? I use version 5.8.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire