Search code examples
laraveleloquentlaravel-7laravel-query-builderlaravel-notification

How to notify all admins by selecting the status_id of the records from the specified table when the user updated the record via e-mail


I using Laravel 7 and I have the topics table that I have 5 status these statuses they are the foreign key from the statuses table.

Topics Table

id topic owner_id status_id
1 A 1 2
2 B 2 6
3 C 3 2
4 D 4 6

Statuses Table

id name
1 Draft
2 Waiting for topic approval
3 Edit the topic
4 Do not approve the topic
5 Approved topic
6 Waiting for scoring
7 Approved score

I want to notify all admins (user_role=1 on the User Table) when users update a record where status_id = 2 or 6 via e-mail.

enter image description here

Thank you in advance. I’m looking forward to your reply.


Solution

  • lets say a user is editing Topic with id 1.

    // import classes in your controller
    use Illuminate\Support\Facades\Notification;
    use App\Notifications\TopicUpdateNotification;
     
    public function update(Request $request, $id)
    {
        // some validation if needed
    
        $topic = Topic::find($id);
        $status = $topic->status_id;
        $topic->update([
            'topic' => $request->topic,
            // add any other column you want to update
        ]);
        
        // now we are checking if the topic status was 2 or 6
        if ($status == 2 || $status == 6) {
            // finding all the admins
            $admins = User::where('user_role', 1)->get();
            $user = auth()->user();
            // sending notification via Notification Facade
            Notification::send($admins, new TopicUpdateNotification($topic, $user));
        }
    }
    

    we have used a class named TopicUpdateNotification. this is a notification class and we have to create it with the artisan command.

    php artisan make:notification TopicUpdateNotification
    

    you will find it in your project's app/Notifications directory. content for this class

    <?php
    
    namespace App\Notifications;
    
    use Illuminate\Bus\Queueable;
    use Illuminate\Contracts\Queue\ShouldQueue;
    use Illuminate\Notifications\Messages\MailMessage;
    use Illuminate\Notifications\Notification;
    
    class TopicUpdateNotification extends Notification
    {
        use Queueable;
    
        /**
         * Create a new notification instance.
         *
         * @return void
         */
        public function __construct($topic, $user)
        {
            $this->topic = $topic;
            $this->user = $user;
        }
    
        /**
         * Get the notification's delivery channels.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function via($notifiable)
        {
            return ['mail'];
        }
    
        /**
         * Get the mail representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return \Illuminate\Notifications\Messages\MailMessage
         */
        public function toMail($notifiable)
        {
            return (new MailMessage)
            ->view(
                'topic_update_email', ['topic' => $this->topic, 'user' => $this->user]
            )
            ->from('support@yourcompany.com', 'Your Company Name') // you can omit this line if you have valid MAIL_FROM_ADDRESS and MAIL_FROM_NAME in your .env
            ->subject('Topic Updated');
        }
    
        /**
         * Get the array representation of the notification.
         *
         * @param  mixed  $notifiable
         * @return array
         */
        public function toArray($notifiable)
        {
            return [
                //
            ];
        }
    }
    

    and finally make a blade file in the views folder topic_update_email.blade.php

    <!DOCTYPE html>
    <html>
    
    <head>
        <title></title>
    </head
    <body>
        <h1>User {{ $user->name }} updated the topic {{ $topic->id }}</h1>
    </body>
    </html>
    

    you can find complete laravel notification doc here