Message, 2 => Group Message * @property-read mixed $is_group * @property-write mixed $raw * @method static Builder|Conversation whereToType($value) * @method static Builder|Conversation message() * @property int|null $reply_to * @property-read Collection|GroupMessageRecipient[] $readByAll * @property-read Collection|GroupMessageRecipient[] $readBy * @property-read Collection|MessageAction $messageAction * @property-read int|null $read_by_all_count * @method static Builder|Conversation whereReplyTo($value) * @property-read Conversation|null $replyMessage */ class Conversation extends Model { use ImageTrait; /** * @var string */ public $table = 'conversations'; public $fillable = [ 'from_id', 'to_id', 'message', 'message_type', 'status', 'file_name', 'to_type', 'reply_to', 'url_details', ]; /** * The attributes that should be casted to native types. * * @var array */ protected $casts = [ 'from_id' => 'integer', 'reply_to' => 'integer', 'to_id' => 'string', 'message' => 'string', 'message_type' => 'integer', 'file_name' => 'string', 'to_type' => 'string', 'url_details' => 'array', 'status' => 'integer', ]; public static $rules = [ 'to_id' => 'required|string', 'message' => 'required|string', ]; // time from now in minuts protected $appends = ['time_from_now_in_min', 'is_group']; const LIMIT = 5000; const PATH = 'conversation'; const MEDIA_IMAGE = 1; const MEDIA_PDF = 2; const MEDIA_DOC = 3; const MEDIA_VOICE = 4; const MEDIA_VIDEO = 5; const YOUTUBE_URL = 6; const MEDIA_TXT = 7; const MEDIA_XLS = 8; const MEDIA_ZIP = 10; const MEDIA_RAR = 11; const MESSAGE_TYPE_BADGES = 9; const MEDIA_MESSAGE_TYPES = [ self::MEDIA_IMAGE, self::MEDIA_PDF, self::MEDIA_DOC, self::MEDIA_VOICE, self::MEDIA_VIDEO, self::YOUTUBE_URL, self::MEDIA_TXT, self::MEDIA_XLS, self::MEDIA_ZIP, self::MEDIA_RAR, ]; public function getMessageAttribute($value) { if (! empty($this->file_name)) { return $this->imageUrl(self::PATH.DIRECTORY_SEPARATOR.$value); } return $value; } protected $with = ['replyMessage']; /** * @param $value * * @return string */ public function getPhotoUrlAttribute($value) { if (! empty($value)) { return $this->imageUrl(User::$PATH.DIRECTORY_SEPARATOR.$value); } return asset('assets/images/avatar.png'); } public function getIsGroupAttribute() { if ($this->to_type == Group::class) { return 1; } return 0; } /** * @return string */ public function getTimeFromNowInMinAttribute() { return Carbon::now()->diffInMinutes($this->created_at); } /** * @return BelongsTo */ public function sender() { return $this->belongsTo(User::class, 'from_id'); } /** * @return BelongsTo */ public function receiver() { return $this->belongsTo(User::class, 'to_id'); } public function scopeMessage(Builder $query) { return $query->where('to_type', self::class); } public function scopeGroup(Builder $query) { return $query->where('to_type', Group::class); } /** * @return BelongsTo */ public function user() { return $this->belongsTo(User::class, 'user_id'); } /** * @return BelongsTo */ public function group() { return $this->belongsTo(Group::class, 'to_id'); } /** * @return HasMany */ public function readByAll() { return $this->hasMany(GroupMessageRecipient::class, 'conversation_id')->whereNull('read_at'); } /** * @return HasMany */ public function readBy() { return $this->hasMany(GroupMessageRecipient::class, 'conversation_id', 'id'); } /** * @return BelongsTo */ public function replyMessage() { return $this->belongsTo(self::class, 'reply_to'); } /** * @return BelongsTo */ public function archiveConversation() { return $this->belongsTo(ArchivedUser::class, 'to_id', 'owner_id')->where('archived_by', '=', getLoggedInUserId()); } /** * @return BelongsTo */ public function archiveUsers() { return $this->belongsTo(ArchivedUser::class, DB::raw('user_id'), 'owner_id')->where('archived_by', '=', getLoggedInUserId()); } /** * @return \Illuminate\Database\Eloquent\Relations\HasOne */ public function messageAction() { return $this->hasOne(MessageAction::class, 'conversation_id')->where('deleted_by', getLoggedInUserId()); } // public function getReadByAllCountAttribute() // { // return GroupMessageRecipient::whereConversationId($this->id)->whereNull('read_at')->count(); // } }