how can I render laravel mailable to variable - laravel

I need to get the html of a Mailable class
$mail->html = (new InactivityReminder())->"do something to output the html"
There seems to be no toHtml method or something similar.
Mailable:
class InactivityReminder extends Mailable
{
use Queueable, SerializesModels;
public function __construct()
{
}
public function build()
{
return $this->markdown('mail.it.inactivityReminder');
}
}

In 5.5 the render() method will render the view that you return in Mailable:
$html = (new InactivityReminder)->render()

The interface does not provide that functionality. That means relying on it can cause problems when updating Laravel in the future. If this is not an issue for you you can use the render() method which returns a View object and then call render() on that.
So:
(new InactivityReminder)->render()->render()
alternatively:
(string) (new InactivityReminder)->render()
as __toString() on View will call it's render method.

For fully customizable mails in Laravel, I've had better luck using Mailables rather than Notifiables. Here's what worked for me:
Mailable class:
use Illuminate\Mail\Mailable;
class MyMail extends Mailable
{
public function __construct($someData) {
$this->someData = $someData;
}
public function build() {
return $this->markdown('mail/my-mail-view')
->with([
"someData" => $this->someData
]);
}
}
Mail view (resources/view/mail/my-mail-view.blade.php):
#extends('mail/mail-layout')
#section('content')
<p>Hi {{ $someData->first_name }}</p>
#endsection
Mail layout (resources/view/mail/mail-layout.blade.php):
<div class="myMailBody">
#yield('content')
</div>
Mail css file (resources/views/vendor/mail/html/themes/default.css):
(assuming you already ran "php artisan vendor:publish --tag=laravel-mail")
.myMailBody {
// css styles here
}
Code in controller:
$previewMailHtml = (string) (new \Illuminate\Mail\Markdown(view(), config('mail.markdown')))->render('mail/my-mail-view', [
"someData" => $someData
]);

Related

Laravel error when sending email notification

Laravel Version: 8.78.1
PHP Version: 8.0.10
I've created a custom command to run on a schedule and email a notification.
My Command class handle method:
public function handle()
{
$sql = "SELECT * FROM Licences WHERE (Expired = 1)";
$list = DB::select($sql);
return (new NotifyExpiredLicences($list))->toMail('me#gmail.com');
}
My notification method:
public function toMail($notifiable)
{
return (new MailMessage)
->subject('Clients with Expired Licences')
->markdown('vendor/notifications/expiredlicences',
['clients' => $this->list, 'toname' => 'Me']);
}
Whenever I test this by running it manually with php artisan email:expired-licences I get the following error Object of class Illuminate\Notifications\Messages\MailMessage could not be converted to int from my command class in the handle method.
However, the preview of my email works fine & displays as expected:
Route::get('/notification', function () {
return (new SendExpiredLicences())->handle();
});
If I remove the return statement from my handle() method, then although I get no errors, neither in my console or in storage\logs, also the preview stops working.
At this point I'm sure I've missed something important from the way this is supposed to be done, but after going through the Laravel docs and looking at online tutorials/examples, I've no idea what.
I've got everything working - though not entirely sure it's the "Laravel way".
If anyone's got suggestions for improving it - add a comment or new answer and I'll try it out.
Console\Kernel.php:
protected function schedule(Schedule $schedule)
{
$schedule->command('email:expired-licences')
->weekdays()
->at('08:30');
}
App\Console\Commands\SendExpiredLicences.php:
class SendExpiredLicences extends Command
{
protected $signature = 'email:expired-licences';
protected $description = 'Email a list of expired licences to Admin';
private $mail;
public function _construct()
{
$clients = DB::select("[Insert SQL here]");
$this->mail = (new NotifyExpiredLicences($clients))->toMail('admin#example.com');
parent::__construct();
}
public function handle()
{
Mail::to('admin#example.com')->send($this->mail);
return 0;
}
public function preview()
{
return $this->mail;
}
}
App\Notifications\NotifyExpiredLicences.php:
class NotifyExpiredLicences extends Notification
{
public function __construct(protected $clients)
{
}
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new Mailable($this->clients));
}
}
App\Mail\ExpiredLicences.php:
class ExpiredLicences extends Mailable
{
public function __construct(private $clients)
{
}
public function build()
{
return $this
->subject('Clients with Expired Licences')
->markdown('emails/expiredlicences',
['clients' => $this->clients, 'toname' => 'Admin']);
}
}
resources\views\emails\expiredlicences.blade.php:
#component('mail::message')
# Hi {!! $toname !!},
#component('mail::table')
| Client | Expired |
| ------------- | --------:|
#foreach ($clients as $client)
|{!! $client->CompanyName !!} | {!! $client->Expired !!}|
#endforeach
#endcomponent
<hr />
Thanks, {!! config('app.name') !!}
#endcomponent
For previewing with the browser routes\web.php:
Route::get('/notification', function () {
return (new SendExpiredLicences())->preview();
});
Ok just to save more commenting, here's what I'd recommend doing. This is all based on the Laravel docs, but there are multiple ways of doing it, including what you've used above. I don't really think of them as "right and wrong," more "common and uncommon."
Console\Kernel.php: I'd keep this mostly as-is, but pass the email to the command from a config file, rather than having it fixed in the command.
use App\Console\Commands\SendExpiredLicences;
…
protected function schedule(Schedule $schedule)
{
$recipient = config('myapp.expired.recipient');
$schedule->command(SendExpiredLicences::class, [$recipient])
->weekdays()
->at('08:30');
}
config/myapp.php:
<?php
return [
'expired' => [
'recipient' => 'admin#example.com',
],
];
App\Console\Commands\SendExpiredLicences.php: update the command to accept the email address as an argument, use on-demand notifications, and get rid of preview() method. Neither the command or the notification need to know about the client list, so don't build it yet.
<?php
namespace App\Console\Commands;
use App\Console\Command;
use App\Notifications\NotifyExpiredLicences;
use Illuminate\Support\Facade\Notification;
class SendExpiredLicences extends Command
{
protected $signature = 'email:expired-licences {recipient}';
protected $description = 'Email a list of expired licences to the given address';
public function handle()
{
$recip = $this->argument('recipient');
Notification::route('email', $recip)->notify(new NotifyExpiredLicences());
}
}
App\Notifications\NotifyExpiredLicences.php: the toMail() method should pass the notifiable object (i.e. the user getting notified) along, because the mailable will be responsible for adding the To address before the thing is sent.
<?php
namespace App\Notifications;
use App\Mail\ExpiredLicenses;
use Illuminate\Notifications\Notification;
class NotifyExpiredLicences extends Notification
{
public function via($notifiable)
{
return ['mail'];
}
public function toMail($notifiable)
{
return (new ExpiredLicenses($notifiable));
}
}
App\Mail\ExpiredLicences.php: since the mail message actually needs the list of clients, this is where we build it. We get the recipient here, either from the user's email or the anonymous object.
<?php
namespace App\Mail;
use App\Models\Client;
use Illuminate\Notifications\AnonymousNotifiable;
class ExpiredLicences extends Mailable
{
private $email;
public function __construct(private $notifiable)
{
// this allows the notification to be sent to normal users
// not just on-demand
$this->email = $notifiable instanceof AnonymousNotifiable
? $notifiable->routeNotificationFor('mail')
: $notifiable->email;
}
public function build()
{
// or whatever your object is
$clients = Client::whereHas('licenses', fn($q)=>$q->whereExpired(1));
return $this
->subject('Clients with Expired Licences')
->markdown(
'emails.expiredlicences',
['clients' => $clients, 'toname' => $this->notifiable->name ?? 'Admin']
)
->to($this->email);
}
}
For previewing with the browser routes\web.php:
Route::get('/notification', function () {
// create a dummy AnonymousNotifiable object for preview
$anon = Notification::route('email', 'no#example.com');
return (new ExpiredLicencesNotification())
->toMail($anon);
});

Laravel Livewire model property binding

This is my Livewire component
<?php
namespace App\Http\Livewire\Dashboard;
use App\Models\Post;
use Livewire\Component;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
class EditPost extends Component
{
use AuthorizesRequests;
public ?Post $postSingle = null;
protected $rules = [
'postSingle.priority' => 'digits_between:1,5',
];
public function setPriority(Post $postSingle, $priority)
{
$this->authorize('update', $postSingle);
$this->postSingle->priority = $priority;
}
}
In my blade view I have
<button wire:click="setPriority({{ $postSingle->id }}, 4)"></button>
and somewhere else I show the priority with {{ $postSingle->priority }}
The reason why I don't do model property binding directly with wire:model="postSingle.priority" is that I want to run $this->authorize('update', $postSingle);.
What happens with the code below is that if I click the button, $postSingle->priority in the blade view is updated, but the Post record is not updated in my database. What am I missing?
You appear to have overlooked actually saving the record.
public function setPriority(Post $postSingle, $priority)
{
$this->authorize('update', $postSingle);
$this->postSingle->priority = $priority;
// save the record
$this->postSingle->save();
}

How to pass Eloquent Data to blade component?

Hello,
I have a blade Layout called:
profile.blade.php // component
And I have a blade file infos which extends from profile.blade.php component.
On my controller, I have a profile method:
public method profile(User $user) {
return view('infos.blade.php', compact('user'));
}
When I try to use the variable user to the profile.blade.php component, I have an error that says "undefined user variable"
My question is how can I get the data I received from my controller to Blade Component profle ?
Because that part of component will be used many times.
Thanks.
in your app/view/components/profile-
<?php
namespace App\View\Components;
use Illuminate\View\Component;
class profile extends Component
{
public $user; //for consistency
public function __construct($user)
{
$this->user = $user;
}
public function render()
{
return view('components.profile');
}
}
now the component can render a variable ($user),
in your info's blade/infos.blade.php you can feed the
variable to the component like --
<x-profile :user='$user' ></x-profile>
i understand the question was posted quite a long ago
but i had the same issue and the answers posted here
didn't work for me but this method did so.....it may help somebody.
As it's a component you have to pass user parameter each time you call it, for example
#component('profile', ['user' => $user]) #endcomponent // here $user comes from controller

lumen observer is not working on the eloquent model

I am using lumen 5.5. I am trying to make the observer called on while updating/deleting the model. When i tried that with user model, observer is not calling. When i did that with the events everything works fine. It's not even shows any errors.
Here is my code:
AppServiceProvider.php
....
use App\Models\User;
use App\Observers\UserObserver;
...
public function boot() {
User::observe(UserObserver::class);
}
App\Models\User.php
...
public static function changeCustomerStatus(int $customerID): int{
$customer = self::where([
'id' => $customerID,
'user_type' => app('config')->get('user_type.CUSTOMER')
])
->first();
if ($customer) {
$customer->status = $customer->status == app('config')->get('status.ACTIVE') ? app('config')->get('status.DEACTIVE') : app('config')->get('status.ACTIVE');
if ($customer->save()) {
return $customer->status;
}
return 0;
}
else
return 0;
}
...
App\Observers\UserObserver.php
<?php
namespace App\Observers;
use App\Models\User;
class UserObserver {
public function updated(User $user) {
if ($user->status === app('config')->get('status.DEACTIVE')) {
app('log')->info('updated');
}
}
public function saved(User $user) {
if ($user->status === app('config')->get('status.DEACTIVE')) {
app('log')->info('saved');
}
}
public function deleted(User $user) {
app('log')->info('deleted');
}
}
I even did the composer dump-autoload. But no luck
Lumen doesn't have observe feature.
You can use Events instead or make custom observer and call its functions from your code.
Read docs here - Events
Lumen does not have model observers the way Laravel does. I agree with using events or implementing your custom observers. If you choose to go with the latter, here is a post that might help.
https://link.medium.com/ZHsJwJuvC5

How to use a View Composer variable in the controller

I am trying to use the View Composer $view_name variable in same controller but it is giving me error.
It is only available to all views, but not for controllers
I want to use that variable in the same controller.
My controller:
public function boot()
{
view()->composer('*', function($view){
$view_name = str_replace('.', '-', $view->getName());
view()->share('view_name', $view_name);
});
$page_name=substr(strrchr(url()->current(),"/"),1);
if($page_name==request()->server('HTTP_HOST')) {
$keywords=keyword::where('page_name','index')->where('active','1')->first();
} else {
$keywords=keyword::where('page_name',$page_name)->where('active','1')->first();
}
}
You can create a Middleware to populate a Configuration variable, configuration is always available from all the framework.
public function handle()
{
config(['myCustomConfig.email' => 'me#example.com']);
}
On your controller
$data = config('myCustomConfig.email');
On your view file
<div>{{ config('myCustomConfig.email') }} </div>

Resources