MessageObserver.php 1.88 KB
<?php

namespace App\Observers;

use App\Mail\SendMessage;
use App\Mail\SendVacancyMessage;
use App\Models\Ad_employer;
use App\Models\Message;
use App\Models\User;
use Illuminate\Support\Facades\Mail;

class MessageObserver
{

    /**
     * Handle the Message "created" event.
     *
     * @param  \App\Models\Message  $message
     * @return void
     */
    public function created(Message $message)
    {

        $toUserId = $message->to_user_id;
        $fromUserId = $message->user_id;
        $toUser =User::query()->where('id',$toUserId)->first();
        $fromUser =User::query()->where('id',$fromUserId)->first();


        if ($toUser && !empty($toUser->subscribe_email) && $toUser->subscribe==1 ){

            if ( (int) $message->ad_employer_id === 0 ){

                Mail::to($toUser->subscribe_email)->send(new SendMessage($message,$fromUser));

            }else{
                 $vacancy=Ad_employer::find($message->ad_employer_id);
                Mail::to($toUser->subscribe_email)->send(new SendVacancyMessage($message,$fromUser,$vacancy));

            }

        }
    }

    /**
     * Handle the Message "updated" event.
     *
     * @param  \App\Models\Message  $message
     * @return void
     */
    public function updated(Message $message)
    {
        //
    }

    /**
     * Handle the Message "deleted" event.
     *
     * @param  \App\Models\Message  $message
     * @return void
     */
    public function deleted(Message $message)
    {
        //
    }

    /**
     * Handle the Message "restored" event.
     *
     * @param  \App\Models\Message  $message
     * @return void
     */
    public function restored(Message $message)
    {
        //
    }

    /**
     * Handle the Message "force deleted" event.
     *
     * @param  \App\Models\Message  $message
     * @return void
     */
    public function forceDeleted(Message $message)
    {
        //
    }
}