abkrim's avatar
Level 13

Help with notification mailable

Hello

I have never spent much time on the subject of notifications, and I feel lost.

I can't find a video of the Laravel series that shows me the way or where to see my error.

In my case I have created a notification with php artisan make:notification Internal/ErrorInternal

Errorinternal.php

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

public function toMail($notifiable)
{
     ray($notifiable, $this->data);
     return (new MailMessage)
                 ->error()
                 ->line('An error occurred while importing one of the tables.')
                 ->action('Failed to import Tables', url('admin/command-centers'))
                 ->line('Check the data. You must repeat the process only with the data that failed');
}

email.blade.php

<x-mail::message>
{{-- Greeting --}}
@if (!empty($greeting))
# {{ $greeting }}
@else
@if ($level === 'error')
# @lang('Warning!')
@else
# @lang('Hello!')
@endif
@endif

{{-- Intro Lines --}}
@foreach ($introLines as $line)
{{ $line }}

@endforeach

{{-- Data table --}}
<x-mail::table>
| Name | Manufacturer | City Hall | Error |
| :------ | :---: | :---:| :----- |
| {{ $notification->name }} | | |
</x-mail::table>



{{-- Action Button --}}
@isset($actionText)
<?php
     $color = match($level) {
         'success', 'error' => $level,
         default => 'primary',
     };
?>
<x-mail::button :url="$actionUrl" :color="$color">
{{ $actionText }}
</x-mail::button>
@endisset

{{-- Outro Lines --}}
@foreach ($outroLines as $line)
{{ $line }}

@endforeach

{{-- Salutation --}}
@if (!empty($salutation))
{{ $salutation }}
@else
@lang('Greetings'),<br>
{{ config('app.name') }}
@endif

{{-- Subcopy --}}
@isset($actionText)
<x-slot:subcopy>
@lang(
     "If you're having trouble clicking the \":actionText\" button, copy and paste the URL below\n".
     'into your web browser:',
     [
         'actionText' => $actionText,
     ]
) <span class="break-all">[{{ $displayableActionUrl }}]({{ $actionUrl }})</span>
</x-slot:subcopy>
@endisset
</x-mail::message>

In this file I want to build a table with some data that I pass in the $rowProperties call;

$rows = SimpleExcelReader::create(storage_path('uploads/').$file)
             //->useHeaders(['name', 'manufacturer_id', 'town_hall_id', 'lat', 'lon'])
             ->useDelimiter(';')
             ->getRows()
             ->each(function(array $rowProperties) use ($user) {
                 // Not check correct data
                 try {
                     CommandCenter::create($rowProperties);
                 } catch(Exception $ignored) {
                     Log::error("Error when import excel CommandCenters ". json_encode($rowProperties));
                     $user->notify(new ErrorInternal($rowProperties));
                 }
             });

I have tried several ways to pass the content of the ErrorInternal to the blade template, email.blade.php but I can't do it.

I've seen in some places the way to do it by calling ->view('template.name' ) but I don't get it right either.

And on top of that, I'm short of time.

Any help, video from laracast or from another site?

0 likes
2 replies
tisuchi's avatar

@abkrim IIUC, you are trying to pass data from Notification to Blade, essentially a mail blade file.

You can simply create a mailable class and you are able to access all the public property of the mailable class from the blade. Check more

For example-

$fileCheck = ['some data'];

Notification::route('mail', ['email-recipients, separate by comma'])
    ->notify(new ErrorInternal($fileCheck));

App/Notifications/ErrorInternal.php

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

public function toMail($notifiable)
{
    return (new InternalErrorFound($this->file))
        ->to($notifiable->routes['mail']);
}

email.blade.php

{{ dd($data) }}         // will dump the whole array 
abkrim's avatar
Level 13

A lot of thanks. After seeing some docs, opt to make them mailable instead of a notification.

Easier for me and with more examples.

Mail::to($user)->send(new ImportCommandCenterErrorMail([
     'name' => $rowProperties['name'],
     'error' => $ignored->getMessage(),
]));

Please or to participate in this conversation.