hasOne(Contact::class) ->withDefault(); } /** * @return HasOne */ public function personalData() { return $this ->hasOne(PersonalData::class) ->withDefault(); } /** * @return HasOne */ public function settings() { return $this ->hasOne(Settings::class) ->withDefault(); } /** * @return HasOne */ public function state() { return $this ->hasOne(State::class) ->withDefault(); } /** * @return HasMany */ public function news(): HasMany { return $this->hasMany(News::class); } /** * @return HasMany */ public function newsComments(): HasMany { return $this->hasMany(NewsComment::class); } /** * @return HasMany */ public function questionsAsked(): HasMany { return $this->hasMany(Question::class, 'user_id') ->where('user_id', $this->id); } /** * @return HasMany */ public function questionsAnswered(): HasMany { return $this->hasMany(Question::class, 'answerer_id') ->where('answerer_id', $this->id); } /** * @return HasMany */ public function messagesSent(): HasMany { return $this->hasMany(Message::class, 'user_id') ->orderBy('created_at', 'DESC') ->orderBy('id', 'DESC'); } /** * @return HasMany|QueryBuilder */ public function messagesReceived(): HasMany { return $this->hasMany(Message::class, 'receiver_id') ->orderBy('read') ->orderBy('created_at', 'DESC') ->orderBy('id', 'DESC'); } /** * Returns a HasMany relation for all messages sent or received by the user. * * @return HasMany */ public function messages(): HasMany { return $this->messagesSent() ->union($this->messagesReceived()) ->orderBy('read') ->orderBy('id', 'DESC'); } }