Amandeep Singh
Amandeep Singh

Reputation: 274

Laravel Mail Undefined Variable

I am trying to send email using Laravel Mail & getting Undefined variable: data in view. Here is my controller code.

    $insured        =   Claim::where('id', '=', $claim_id)->first()->toArray();
    $workshop       =   Workshop::where('id', '=', $insured['workshop_id'])->first()->toArray();
    $data           =   Document::whereRaw('claim_id = ' . $claim_id . ' AND (documents.status = 10 OR documents.status = 12)')
                        ->join('docs','documents.doc_id', '=', 'docs.id')
                        ->join('claims','documents.claim_id', '=', 'claims.id')
                        ->get(array('docs.email_text', 'claim_of', 'vehicle_no'))->toArray();
    Mail::send('emails.workshop', $data, function($message)
    {
        $message->to('[email protected]')->subject('Testing');
    });

View

{{ $data[0]['claim_of'] }}

<br />

{{ $data[0]['vehicle_no'] }}

<br />


    <?php $i = 1 ; ?>
    @foreach($data as $key => $claim)
        <p> {{ $i }}. {{ $claim->email_text }}</p>
        <?php $i++ ; ?>
    @endforeach

When I replace Laravel Mail code with

            return View::make('emails.workshop')->with('data', $data);

everything works perfectly.

Upvotes: 0

Views: 6107

Answers (1)

lukasgeiter
lukasgeiter

Reputation: 153140

Parameters have to be passed in as key value array

Mail::send('emails.workshop', array('data' => $data), function($message)
{
    $message->to('[email protected]')->subject('Testing');
});

Laravel Docs

There's a nice PHP function called compact that does this automatically, based on the variable name.

So compact('data') returns array('data' => $data) (if $data is defined)

Upvotes: 6

Related Questions