2018-10-06 14:15:54 +02:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Engelsystem\Models\User;
|
|
|
|
|
2019-11-06 13:43:57 +01:00
|
|
|
use Carbon\Carbon;
|
2018-10-06 14:15:54 +02:00
|
|
|
use Engelsystem\Models\BaseModel;
|
2019-12-12 21:30:28 +01:00
|
|
|
use Engelsystem\Models\Message;
|
2019-11-10 21:30:26 +01:00
|
|
|
use Engelsystem\Models\News;
|
2019-11-12 21:49:43 +01:00
|
|
|
use Engelsystem\Models\NewsComment;
|
2019-12-03 20:09:37 +01:00
|
|
|
use Engelsystem\Models\Question;
|
2019-11-12 21:49:43 +01:00
|
|
|
use Illuminate\Database\Eloquent\Collection;
|
2019-10-31 20:02:34 +01:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasMany;
|
2018-10-06 14:15:54 +02:00
|
|
|
use Illuminate\Database\Eloquent\Relations\HasOne;
|
2019-11-06 13:43:57 +01:00
|
|
|
use Illuminate\Database\Query\Builder as QueryBuilder;
|
2018-10-06 14:15:54 +02:00
|
|
|
|
|
|
|
/**
|
2019-11-06 13:43:57 +01:00
|
|
|
* @property integer $id
|
|
|
|
* @property string $name
|
|
|
|
* @property string $email
|
|
|
|
* @property string $password
|
|
|
|
* @property string $api_key
|
|
|
|
* @property Carbon|null $last_login_at
|
|
|
|
* @property Carbon $created_at
|
|
|
|
* @property Carbon $updated_at
|
2018-10-06 14:15:54 +02:00
|
|
|
*
|
2019-11-06 13:43:57 +01:00
|
|
|
* @property-read QueryBuilder|Contact $contact
|
|
|
|
* @property-read QueryBuilder|PersonalData $personalData
|
|
|
|
* @property-read QueryBuilder|Settings $settings
|
|
|
|
* @property-read QueryBuilder|State $state
|
2019-12-01 01:32:20 +01:00
|
|
|
* @property-read Collection|News[] $news
|
2019-11-12 21:49:43 +01:00
|
|
|
* @property-read Collection|NewsComment[] $newsComments
|
2019-12-01 01:32:20 +01:00
|
|
|
* @property-read int|null $news_count
|
|
|
|
* @property-read int|null $news_comments_count
|
2018-10-06 14:15:54 +02:00
|
|
|
*
|
2019-12-01 01:32:20 +01:00
|
|
|
* @method static QueryBuilder|User[] whereId($value)
|
2019-11-06 13:43:57 +01:00
|
|
|
* @method static QueryBuilder|User[] whereName($value)
|
|
|
|
* @method static QueryBuilder|User[] whereEmail($value)
|
|
|
|
* @method static QueryBuilder|User[] wherePassword($value)
|
|
|
|
* @method static QueryBuilder|User[] whereApiKey($value)
|
|
|
|
* @method static QueryBuilder|User[] whereLastLoginAt($value)
|
|
|
|
* @method static QueryBuilder|User[] whereCreatedAt($value)
|
|
|
|
* @method static QueryBuilder|User[] whereUpdatedAt($value)
|
2019-12-03 20:09:37 +01:00
|
|
|
*
|
|
|
|
* @property-read Collection|Question[] $questionsAsked
|
|
|
|
* @property-read Collection|Question[] $questionsAnswered
|
2019-12-12 21:30:28 +01:00
|
|
|
* @property-read Collection|Message[] $messagesReceived
|
|
|
|
* @property-read Collection|Message[] $messagesSent
|
|
|
|
* @property-read Collection|Message[] $messages
|
2018-10-06 14:15:54 +02:00
|
|
|
*/
|
|
|
|
class User extends BaseModel
|
|
|
|
{
|
|
|
|
/** @var bool enable timestamps */
|
|
|
|
public $timestamps = true;
|
|
|
|
|
|
|
|
/** The attributes that are mass assignable */
|
|
|
|
protected $fillable = [
|
|
|
|
'name',
|
|
|
|
'password',
|
|
|
|
'email',
|
|
|
|
'api_key',
|
|
|
|
'last_login_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var array The attributes that should be hidden for serialization */
|
|
|
|
protected $hidden = [
|
|
|
|
'api_key',
|
|
|
|
'password',
|
|
|
|
];
|
|
|
|
|
|
|
|
/** @var array The attributes that should be mutated to dates */
|
|
|
|
protected $dates = [
|
|
|
|
'last_login_at',
|
|
|
|
];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasOne
|
|
|
|
*/
|
|
|
|
public function contact()
|
|
|
|
{
|
|
|
|
return $this
|
|
|
|
->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();
|
|
|
|
}
|
2019-10-31 20:02:34 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function news(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(News::class);
|
|
|
|
}
|
2019-11-12 21:49:43 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function newsComments(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(NewsComment::class);
|
|
|
|
}
|
2019-12-03 20:09:37 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @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);
|
|
|
|
}
|
2019-12-12 21:30:28 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
public function messagesSent(): HasMany
|
|
|
|
{
|
|
|
|
return $this->hasMany(Message::class, 'user_id')
|
|
|
|
->orderBy('created_at', 'DESC')
|
|
|
|
->orderBy('id', 'DESC');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return HasMany
|
|
|
|
*/
|
|
|
|
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');
|
|
|
|
}
|
2018-10-06 14:15:54 +02:00
|
|
|
}
|