engelsystem/src/Models/User/User.php

229 lines
5.9 KiB
PHP
Raw Normal View History

2018-10-06 14:15:54 +02:00
<?php
namespace Engelsystem\Models\User;
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;
use Engelsystem\Models\News;
2019-11-12 21:49:43 +01:00
use Engelsystem\Models\NewsComment;
2020-11-15 18:47:30 +01:00
use Engelsystem\Models\OAuth;
2019-12-03 20:09:37 +01:00
use Engelsystem\Models\Question;
2020-09-12 19:45:25 +02:00
use Engelsystem\Models\Worklog;
2019-11-12 21:49:43 +01:00
use Illuminate\Database\Eloquent\Collection;
2021-06-29 00:27:57 +02:00
use Illuminate\Database\Eloquent\Factories\HasFactory;
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;
use Illuminate\Database\Query\Builder as QueryBuilder;
2018-10-06 14:15:54 +02:00
/**
* @property int $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
*
* @property-read QueryBuilder|Contact $contact
2021-12-19 18:38:42 +01:00
* @property-read QueryBuilder|License $license
* @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
2020-11-15 18:47:30 +01:00
* @property-read Collection|OAuth[] $oauth
2020-09-12 19:45:25 +02:00
* @property-read Collection|Worklog[] $worklogs
* @property-read Collection|Worklog[] $worklogsCreated
2019-12-01 01:32:20 +01:00
* @property-read int|null $news_count
* @property-read int|null $news_comments_count
2020-11-15 18:47:30 +01:00
* @property-read int|null $oauth_count
2020-09-12 19:45:25 +02:00
* @property-read int|null $worklogs_count
* @property-read int|null $worklogs_created_count
2018-10-06 14:15:54 +02:00
*
2019-12-01 01:32:20 +01:00
* @method static QueryBuilder|User[] whereId($value)
* @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
{
2021-06-29 00:27:57 +02:00
use HasFactory;
2018-10-06 14:15:54 +02:00
/** @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();
}
2021-12-19 18:38:42 +01:00
/**
* @return HasOne
*/
public function license()
{
return $this
->hasOne(License::class)
->withDefault();
}
2018-10-06 14:15:54 +02:00
/**
* @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
2020-11-15 18:47:30 +01:00
/**
* @return HasMany
*/
public function oauth(): HasMany
{
return $this->hasMany(OAuth::class);
}
2020-09-12 19:45:25 +02:00
/**
* @return HasMany
*/
public function worklogs(): HasMany
{
return $this->hasMany(Worklog::class);
}
/**
* @return HasMany
*/
public function worklogsCreated(): HasMany
{
return $this->hasMany(Worklog::class, 'creator_id');
}
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|QueryBuilder
2019-12-12 21:30:28 +01:00
*/
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
}