Mail problem rendering view with data with two arrays

Hello. I have a row action which lets users send email with attached Pdf with some info about the record. Everything works fine. Now I want the user can change subject and body of the email and I have included a form and user can type.
Action::make('enviarporEmail')->label('')->modalDescription('ddd')
->form([
TextInput::make('email')
->label('Email al que enviar:')
->email()
->default(Auth()->user()->email)
->required(),
TextInput::make('asunto')
...
Action::make('enviarporEmail')->label('')->modalDescription('ddd')
->form([
TextInput::make('email')
->label('Email al que enviar:')
->email()
->default(Auth()->user()->email)
->required(),
TextInput::make('asunto')
...
And it works fine till then. I launch the mail with the data:
->action(function (array $data,Expediente $record) {
$receivers = $data['email'];
$expediente = Expediente::find($record->id);
Mail::to($receivers)
->send(new ExpedienteCarpetaEmail ($expediente, $data));
})

And I can check inside the __construct of the mail controller that data arrives correctly:
public $expediente;
public $data;
public function __construct(Expediente $expediente, $data)
{
// dd($data);
if (!$data['asunto']) {
$this->subject('Información del expediente ' . $expediente->numExp);
} else {
$this->subject($data['asunto']);
}
$this->expediente = $expediente;
Pdf::view('expedientes.carpetaA4', ['expediente' => $expediente])
->format('a4')
->save('carpeta-'. $expediente->numExp .'.pdf');
// $this->content('expedientes.email', $data = $data);
$this->content(fn () => view('expedientes.email', ['expediente' => $expediente, 'data' => $data]));
$this->attach('carpeta-'. $expediente->numExp .'.pdf');
}

And inside the final view (expedientes.email) of the final step it thows an error of:
Undefined variable $data (View: ...
->action(function (array $data,Expediente $record) {
$receivers = $data['email'];
$expediente = Expediente::find($record->id);
Mail::to($receivers)
->send(new ExpedienteCarpetaEmail ($expediente, $data));
})

And I can check inside the __construct of the mail controller that data arrives correctly:
public $expediente;
public $data;
public function __construct(Expediente $expediente, $data)
{
// dd($data);
if (!$data['asunto']) {
$this->subject('Información del expediente ' . $expediente->numExp);
} else {
$this->subject($data['asunto']);
}
$this->expediente = $expediente;
Pdf::view('expedientes.carpetaA4', ['expediente' => $expediente])
->format('a4')
->save('carpeta-'. $expediente->numExp .'.pdf');
// $this->content('expedientes.email', $data = $data);
$this->content(fn () => view('expedientes.email', ['expediente' => $expediente, 'data' => $data]));
$this->attach('carpeta-'. $expediente->numExp .'.pdf');
}

And inside the final view (expedientes.email) of the final step it thows an error of:
Undefined variable $data (View: ...
The view can manage $expediente->whateverfield correctly but $data is undefined. Any ideas, please?
Solution:
I solved it!!! Looking at the official info from Laravel: https://laravel.com/docs/11.x/mail#configuring-the-view ...
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.
Jump to solution
1 Reply
Solution
Albert Lens
Albert Lens3mo ago
I solved it!!! Looking at the official info from Laravel: https://laravel.com/docs/11.x/mail#configuring-the-view I don't have to pass the data in the ->content(...) It has to be added to the content function itself:
public function content(): Content
{
return new Content(
view: 'expedientes.email',
with: ['data' => $this->data],
);
}
public function content(): Content
{
return new Content(
view: 'expedientes.email',
with: ['data' => $this->data],
);
}
The with: is the solution to add extra data apart from the Expediente resource data.
Laravel - The PHP Framework For Web Artisans
Laravel is a PHP web application framework with expressive, elegant syntax. We’ve already laid the foundation — freeing you to create without sweating the small things.