Blame view
app/Models/Chat.php
3.86 KB
712f07ca2 часть задачи по р... |
1 2 3 4 5 6 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; |
2f592b01f сообщения |
7 |
use Carbon\Carbon; |
712f07ca2 часть задачи по р... |
8 9 10 11 12 13 14 15 16 17 18 19 |
class Chat extends Model { use HasFactory; protected $fillable = [ 'user_id', 'to_user_id', 'is_removed', 'is_fixed', 'last_message_date', 'last_message_id', |
f98ccb92c Массова рассылка |
20 21 |
'fixed_time', 'is_admin_chat' |
712f07ca2 часть задачи по р... |
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
]; 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'); } |
a93910932 рефакторинг сообщ... |
39 40 41 42 |
public function unread_messages() { return $this->hasMany(Message::class, 'user_id', 'to_user_id'); } |
df58eaf86 правки |
43 44 45 46 |
public function admin_chat_unread_messages() { return $this->hasMany(Message::class, 'to_user_id', 'to_user_id'); } |
2f592b01f сообщения |
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
public static function pin_chat(int $chat_id, $fixed) { return self::where('id', '=', $chat_id) ->update([ 'is_fixed' => !empty($fixed) ? 1 : 0, 'fixation_date' => !empty($fixed) ? Carbon::now() : null ]); } 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) { |
b425eeabc правки |
69 70 71 72 73 |
$query ->where('to_user_id', '=', $user_id) ->whereNotNull('chat_id_from') ->where('flag_new', '=', 1) ; |
2f592b01f сообщения |
74 75 76 77 78 79 80 81 82 |
}]) ->where('user_id', '=', $user_id) ->where('is_removed', '=', 0) ->orderByDesc('is_fixed') ->orderByDesc('fixation_date') ->orderByDesc('last_message_date') ->paginate(5) ; } |
f98ccb92c Массова рассылка |
83 84 85 86 |
public static function get_user_admin_chat(int $user_id) { return Chat::query() ->with('last_message') |
df58eaf86 правки |
87 88 |
->withCount(['admin_chat_unread_messages' => function ($query) use($user_id) { $query->whereNull('chat_id_from')->where('to_user_id', '=', $user_id)->where('flag_new', '=', 1); |
f98ccb92c Массова рассылка |
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 |
}]) ->where('to_user_id', '=', $user_id) ->where('is_admin_chat', 1) ->first() ; } public static function get_chat_messages(Chat $chat){ if ($chat->is_admin_chat){ return Message::query() ->where('chat_id_to', $chat->id) ->where('to_user_id', $chat->to_user_id) ->orderBy('created_at') ->get() ; } else { 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() ; } } |
712f07ca2 часть задачи по р... |
124 |
} |