简体   繁体   中英

can't access object laravel 5.3

I am using laravle 5.3 mailables for sending emails.I want to pass data to view via with method.

I have create a mailable MytestMail.php

<?php

namespace App\Mail;

use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;

class MytestMail extends Mailable {
    public $dataArray;
    use Queueable,
        SerializesModels;

    /**
     * Create a new message instance.
     *
     * @return void
     */
    public function __construct($dataArray) {
        $this->dataArray= $dataArray;

        //
    }

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build() {

        $address = 'test@gmail.com';
        $name = 'asasa';
        $subject = 'sasasasub';


        return $this->view('emails.myTestMail')
                ->from($address, $name)
                ->cc($address, $name)
                ->bcc($address, $name)
                ->replyTo($address, $name)
                ->subject($subject)
               ->with([
                        'name' => $this->dataArray->name,
                        'password' => $this->dataArray->password,
                        'E_id' => $this->dataArray->E_id,
                        'email' => $this->dataArray->email,


                    ]);
        ;
    }

}

Now i get an error

Trying to get property of non-object

When i print_r $this->dataArray i got an array with values

Array ( [name] => sdsdsds [E_id] => 0123 [password] => sdsd [username]
=> sdsdsdsds@sgxcxcxmail.com )

Please help me...Why i get this error message?

当您使用print_r时,它会告诉您->Array<- ( [name] => sdsdsds [E_id] => 0123 [password] => sdsd [username]=> sdsdsdsds@sgxcxcxmail.com ) ,因此在访问时变量$dataArray确保以数组形式访问它,例如$this->dataArray['name']$this->dataArray['password']等。

The issue is here

'name' => $this->dataArray->name,

you are trying to access name from $this-dataArray even though the name is an index of the array hence it is not a property of an object. The error message is self-explanatory.

Trying to get property of non-object

What You should do instead is

'name' => $this->dataArray['name'],

Same goes to other array elements mentioned in your question.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM