Dan Knights
Dan Knights

Reputation: 8368

Unable to access property - Laravel

I'm trying to send emails in Laravel.

I have my Mailable:

    public $message;

    public function __construct($message)
    {
        $this->message = $message;
    }

    public function build()
    {
        return $this->subject('Message from B&B Vintage Collectibles')->view('mail.direct');
    }

Controller:

namespace App\Http\Controllers;

use App\Contact;
use App\Mail\DirectMailable;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Mail;
use App\Http\Controllers\Controller;

class DirectController extends Controller
{
    public function __invoke(Request $request)
    {
        $user = $request->get('email');

        $message = new Contact();

        $message->body = $request->get('message');

        Mail::to($user)->send(new DirectMailable($message));

        return response()->json('Message sent', 200);
    }
}

And in direct.blade.php I have this line:

<p>{{ $message->body }}</p>

But when I send the email I get this response:

"Undefined property: Illuminate\Mail\Message::$body..."

I have other email systems setup in almost the exact same way so I can't figure out why this isn't working.

I've tried without the templates and the email sends just fine.

When I dd($message->body) before Mail::to... I get the correct string.

What's happening on the way to my blade file that won't let me access this property?

Upvotes: 0

Views: 121

Answers (2)

Akhtar Munir
Akhtar Munir

Reputation: 1769

I have used in the same way and it works. But I do something like this... Passing the actual data to the view

public function build()
{
    return $this->subject('Message from B&B Vintage Collectibles')->view('mail.direct',[ 'message' => $this->message ]);
}

Upvotes: 0

Dupinder Singh
Dupinder Singh

Reputation: 7769

As discussed on this thread
https://laracasts.com/discuss/channels/laravel/laravel-mailable-not-working?page=1

don't use $message variable, it cause errors in email send in larvel framework.

You can follow this thread for more info.

Upvotes: 1

Related Questions