Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Filth's avatar

Passing $this to Mail::send

Im trying to add a function to a reservation model that will send an email. I need to pass the reservation info to the email template. How would this be achieved? passing the function $this throws a lexical error. should I store $this in another variable/object and pass that?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Mail;

class Reservation extends Model
{
    protected $fillable = [
        'booking_id',
        'leaderFName',
        'leaderLName',
        'telephone',
        'address',
        'address2',
        'town',
        'county',
        'postcode',
        'party',
        'final_reminder'
    ];

    public function booking()
    {
        return $this->belongsTo('App\Booking');
    }

    public function user()
    {
        return $this->belongsTo('App\User');
    }

    public function transactions()
    {
        return $this->hasMany('App\Transaction');
    }

    public function send_finalReminder()
    {
        Mail::send('emails.paymentDue', ['reservation' => $this], function ($m) use ($this) {
                    $m->to($this->user->email, $this->user->name)->subject('Balance Due');
                });
    }
}
0 likes
1 reply
RoboRobok's avatar

You have this error, because use expects a "normal" variable. This is one of PHP's quirks. Try this:

$self = $this;

And then:

function ($m) use ($self) {

Please or to participate in this conversation.