$users * @property-read int|null $users_count * * @method static \Illuminate\Database\Eloquent\Builder|Inbox newModelQuery() * @method static \Illuminate\Database\Eloquent\Builder|Inbox newQuery() * @method static \Illuminate\Database\Eloquent\Builder|Inbox query() * @method static \Illuminate\Database\Eloquent\Builder|Inbox whereCreatedAt($value) * @method static \Illuminate\Database\Eloquent\Builder|Inbox whereId($value) * @method static \Illuminate\Database\Eloquent\Builder|Inbox whereLastMessage($value) * @method static \Illuminate\Database\Eloquent\Builder|Inbox whereLastUserId($value) * @method static \Illuminate\Database\Eloquent\Builder|Inbox whereUpdatedAt($value) * * @mixin \Eloquent */ class Inbox extends Model { protected $fillable = ['last_user_id', 'last_message']; public function users(): BelongsToMany { return $this->belongsToMany(User::class); } public function lastUser(): BelongsTo { return $this->belongsTo(User::class, 'id', 'last_user_id'); } public function messages(): HasMany { return $this->hasMany(Message::class, 'inbox_id'); } public function getRecipientAttribute(): User { // first user in the relationship that is NOT the authenticated user return $this->users->where('id', '!=', auth()->id())->first(); } }