Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can't access object laravel 5.3

I am using laravel 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 = '[email protected]';
        $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]
=> [email protected] )

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

like image 957
Shanu k k Avatar asked Jan 06 '17 09:01

Shanu k k


2 Answers

When you print_r it tells you the data type which is ->Array<- ( [name] => sdsdsds [E_id] => 0123 [password] => sdsd [username]=> [email protected] ), So when accessing the variable $dataArray be sure to access it as an array like so $this->dataArray['name'] or $this->dataArray['password'] etc...

like image 104
Kenziiee Flavius Avatar answered Nov 06 '22 19:11

Kenziiee Flavius


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.

like image 43
Gayan Avatar answered Nov 06 '22 17:11

Gayan