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?
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...
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With