lundi 26 octobre 2015

Undefined property when passing parameter to command via constructor

I modified my App\Commands\SendEmail command like this:

<?php namespace App\Commands;

use App\Commands\Command;

use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;

use Mail;

class SendEmail extends Command implements SelfHandling, ShouldBeQueued {

    use InteractsWithQueue, SerializesModels;

    protected $invoice, $tickets, $extras, $customer;

    /**
     * Create a new command instance.
     *
     * @param $params
     */
    public function __construct($params)
    {
        // Purchase Parameters
        $this->invoice = (object)$params['invoice'];
        $this->tickets = collect($params['tickets']);
        $this->extras = collect($params['extras']);
        $this->customer = (object)$params['customer'];

    }

    /**
     * Execute the command.
     *
     * @return void
     */
    public function handle()
    {
        // Sending Mail
        $from = "mail@myticketgh.com";
        $to = $this->customer->email;

        $params = [
            'invoice' => $this->invoice,
            'tickets' => $this->tickets,
            'extras' => $this->extras,
            'customer' => $this->customer
        ];

        Mail::queue('emails.purchases.event', $params, function ($message) use ($from, $to, $params) {
            $message->from($from)
                    ->to($to)
                    ->subject('Ticket Purchase: '.$params['invoice']->reference_code);
        });

    }

}

This is supposed to send am email to a customer after they have purchased a ticket using my application.

This is the code in the controller for that:

$params = [
    'invoice' => $invoice_details,
    'tickets' => $invoice_tickets,
    'extras' => $invoice_extras,
    'customer' => $invoice_customer ];

$this->dispatch(
    new SendEmail($params) );

My problem now is all those parameter see to be inaccessible or empty, i dunno which, starting at where i have $to = $this->customer->email; I always get this error:

Undefined property: Illuminate\\Database\\Eloquent\\Collection::$first_name

I dunno what I am doing wrong here.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire