Thomas Charlesworth
Thomas Charlesworth

Reputation: 1839

Laravel 5.5 Cannot use object of type Illuminate\Mail\Message as array

I'm building an application on Laravel 5.5 where I'm facing difficulties with mails. My view for mailable:

<h3>Name: {{$message['name']}}</h3> 
<h3>Email: {{$message['email']}}</h3> 
<p>Message: {{$message['message']}}</p>

My mailable class:

namespace App\Mail;

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

class Contact extends Mailable
{
    use Queueable, SerializesModels;

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

    /**
     * Build the message.
     *
     * @return $this
     */
    public function build()
    {
        $object = $this->object;
        $message = [
            'name' => $object['name'],
            'email' => $object['email'],
            'message' => $object['message']
        ];
        return $this->view('mails.contact')->with('message', $message);
    }
}

I'm getting an error of

Cannot use object of type Illuminate\Mail\Message as array

Upvotes: 0

Views: 2958

Answers (2)

Eamon
Eamon

Reputation: 96

Check that you change the variable name of $object to something else.

First it bad form to use the word object as a variable name, and second you might run into unexpected problems with Objects and $object.

And Secondly you are referencing 'message' => $object['message'] where it should be 'message' => $object->message as $object variable is in fact a Object and not an array

Upvotes: 0

Ritesh Khatri
Ritesh Khatri

Reputation: 1301

Message is the instance ofIlluminate\Mail\Message it is used for combining information of data to template. change your $message variable to another one it will solve your issue. I hope it will helps you.

Upvotes: 3

Related Questions