Chat.php 3.57 KB
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Carbon\Carbon;

class Chat extends Model
{
    use HasFactory;

    protected $fillable = [
        'user_id',
        'to_user_id',
        'is_removed',
        'is_fixed',
        'last_message_date',
        'last_message_id',
        'fixed_time',
        'is_admin_chat'
    ];

    public function user() {
        return $this->belongsTo(User::class, 'to_user_id');
    }

    public function worker() {
        return $this->belongsTo(Worker::class, 'to_user_id', 'user_id');
    }

    public function employer() {
        return $this->belongsTo(Employer::class, 'to_user_id', 'user_id');
    }

    public function last_message() {
        return $this->belongsTo(Message::class, 'last_message_id');
    }

    public function unread_messages()
    {
        return $this->hasMany(Message::class, 'user_id', 'to_user_id');
    }

    public function admin_chat_unread_messages()
    {
        return $this->hasMany(Message::class, 'to_user_id', 'to_user_id');
    }

    public static function pin_chat(int $chat_id, $fixed)
    {
        $chat = self::query()->find($chat_id);
        $chat->update([
                'is_fixed' => !empty($fixed) ? 1 : 0,
                'fixation_date' => !empty($fixed) ? Carbon::now() : null
            ]);
        return $chat;
    }

    public static function remove_chat(int $chat_id)
    {
        return self::where('id', '=', $chat_id)
            ->update(['is_removed' => 1]);
    }

    public static function get_user_chats(int $user_id){
        return Chat::query()
            ->with('user')
            ->with('worker')
            ->with('employer')
            ->with('last_message')
            ->withCount(['unread_messages' => function ($query) use($user_id) {
                $query
                    ->where('to_user_id', '=', $user_id)
                    ->whereNotNull('chat_id_from')
                    ->where('flag_new', '=', 1)
                ;
            }])
            ->where('user_id', '=', $user_id)
            ->where('is_removed', '=', 0)
            ->orderByDesc('is_fixed')
            ->orderByDesc('fixation_date')
            ->orderByDesc('last_message_date')
            ->paginate(5)
        ;
    }

    public static function get_user_admin_chat(int $user_id)
    {
        return Chat::query()
            ->with('last_message')
            ->withCount(['unread_messages' => function ($query) use($user_id) {
                $query->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1);
            }])
            ->where('user_id', '=', $user_id)
            ->where('is_admin_chat', 1)
            ->first()
            ;
    }

    public static function get_chat_messages(Chat $chat){

            return Message::query()
                ->where(function ($query) use ($chat) {
                    $query->where('chat_id_from', $chat->id)->orWhere('chat_id_to', $chat->id);
                })
                ->where(function($query) use ($chat) {
                    $query
                        ->where(function($query) use ($chat) {
                            $query->where('user_id', $chat->user_id)->where('to_user_id', $chat->to_user_id);
                        })
                        ->orWhere(function($query) use ($chat) {
                            $query->where('user_id', $chat->to_user_id)->where('to_user_id', $chat->user_id);
                        })
                    ;
                })
                ->OrderBy('created_at')
                ->get()
            ;
    }

}