Nida Akram
Nida Akram

Reputation: 362

Sending variable in mail view

I am currently working on my project in which I want to send variable to my mail view. I have tried many a times but it gives me error. Kindly tell me what I am doing wrong

Code of my Controller

$data = array('to'=> $patientEmail, 'from'=>'[email protected]', 'sender_name'=>'Admin', 'receiver_name'=>$request->name, 'subject'=>'Appointment Cancellled', 'message'=> $request->cancelMessage);
Mail::Send(['html'=>"mail/cancelAppointmentmail"], $data, function($message) use ($data) {
    $message->to($data['to'], $data['receiver_name'])->subject($data['subject']);
    $message->from($data['from'], $data['sender_name']);
});

Code of my view

<p> <?php echo $data['message']; ?></p>

Upvotes: 1

Views: 338

Answers (3)

Douwe de Haan
Douwe de Haan

Reputation: 6646

The second parameter of the Mail::send method are variables that you can use in the view. The key of the value is the name of the variable in the view:

Your array

$data = array(
    'to'=> $patientEmail,
    'from'=>'[email protected]',
    'sender_name'=>'Admin',
    'receiver_name'=>$request->name,
    'subject'=>'Appointment Cancellled',
    'message'=> $request->cancelMessage
);

will result in the view having access to the following variables:

$to = $patientEmail;
$from = '[email protected]';
$sender_name = 'Admin';
$receiver_name = $request->name;
$subject ='Appointment Cancellled';
$message = $request->cancelMessage;

So where you wrote

<p><?php echo $data['message']; ?></p>

You should write

<p><?php echo $message; ?></p>

Or with Blade syntax:

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

I know I'm late to the party, but as others failed to mention the cause of your problem, I wanted to write my answer as well.

Upvotes: 0

Vishnu M Raveendran
Vishnu M Raveendran

Reputation: 411

Try this:

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

If you have html input then:

<p>{!! $message !!}</p>

Upvotes: 0

yrv16
yrv16

Reputation: 2275

Use Blade syntax in your view:

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

Upvotes: 1

Related Questions