- directory refactor

This commit is contained in:
Krzysztof Rewak
2022-01-26 08:24:48 +01:00
parent 6854c7a9f8
commit 9ac804545b
56 changed files with 144 additions and 163 deletions

View File

@@ -0,0 +1,72 @@
<?php
declare(strict_types=1);
namespace Toby\Eloquent\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Illuminate\Support\Carbon;
use Illuminate\Support\Collection;
use Toby\Domain\EmploymentForm;
/**
* @property int $id
* @property string $first_name
* @property string $last_name
* @property string $email
* @property string $avatar
* @property EmploymentForm $employment_form
* @property Carbon $employment_date
* @property Collection $vacationLimits
*/
class User extends Authenticatable
{
use HasFactory;
use Notifiable;
use SoftDeletes;
protected $guarded = [];
protected $casts = [
"employment_form" => EmploymentForm::class,
"employment_date" => "datetime",
];
protected $hidden = [
"remember_token",
];
public function vacationLimits(): HasMany
{
return $this->hasMany(VacationLimit::class);
}
public function scopeSearch(Builder $query, ?string $text): Builder
{
if ($text === null) {
return $query;
}
return $query
->where("first_name", "LIKE", "%{$text}%")
->orWhere("last_name", "LIKE", "%{$text}%")
->orWhere("email", "LIKE", "%{$text}%");
}
public function saveAvatar(string $path): void
{
$this->avatar = $path;
$this->save();
}
public function getFullNameAttribute(): string
{
return "{$this->first_name} {$this->last_name}";
}
}

View File

@@ -0,0 +1,45 @@
<?php
declare(strict_types=1);
namespace Toby\Eloquent\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
/**
* @property int $id
* @property User $user
* @property YearPeriod $yearPeriod
* @property int $days
*/
class VacationLimit extends Model
{
use HasFactory;
protected $guarded = [];
public function hasVacation(): bool
{
return $this->days !== null;
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class);
}
public function yearPeriod(): BelongsTo
{
return $this->belongsTo(YearPeriod::class);
}
public function scopeOrderByUserField(Builder $query, string $field): Builder
{
$userQuery = User::query()->select($field)->whereColumn("vacation_limits.user_id", "users.id");
return $query->orderBy($userQuery);
}
}

View File

@@ -0,0 +1,38 @@
<?php
declare(strict_types=1);
namespace Toby\Eloquent\Models;
use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Support\Collection;
/**
* @property int $id
* @property int $year
* @property Collection $vacationLimits
*/
class YearPeriod extends Model
{
use HasFactory;
protected $fillable = [
"year",
];
public static function current(): ?static
{
/** @var YearPeriod $year */
$year = static::query()->where("year", Carbon::now()->year)->first();
return $year;
}
public function vacationLimits(): HasMany
{
return $this->hasMany(VacationLimit::class);
}
}