mirror of
https://git.meshkee.com/cssd/cssd-back.git
synced 2026-10-11 00:31:07 +03:30
97 lines
2.5 KiB
PHP
97 lines
2.5 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Constants\StatusTypeEnum;
|
|
use App\Services\Media\HasMediaTrait;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Spatie\Image\Exceptions\InvalidManipulation;
|
|
use Spatie\MediaLibrary\HasMedia;
|
|
use Spatie\MediaLibrary\MediaCollections\Models\Media;
|
|
|
|
class Item extends Model implements HasMedia
|
|
{
|
|
use HasMediaTrait;
|
|
|
|
public $fillable = [
|
|
'name', 'size', 'unique_id', 'specific', 'note', 'user_id', 'udi', 'status', 'repair',
|
|
'package_id', 'creator_id'
|
|
];
|
|
|
|
public function scopeIsNotRepaired($query)
|
|
{
|
|
return $query->where('repair', 0);
|
|
}
|
|
|
|
public function package(): BelongsTo
|
|
{
|
|
return $this->belongsTo(Package::class);
|
|
}
|
|
|
|
public function user(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
public function creator(): BelongsTo
|
|
{
|
|
return $this->belongsTo(User::class, "creator_id");
|
|
}
|
|
|
|
public function transfersAll(): MorphMany
|
|
{
|
|
return $this->morphMany(Transfer::class, "transferable")->orderBy("id", "desc");
|
|
}
|
|
|
|
public function transfers(): MorphMany
|
|
{
|
|
return $this->morphMany(Transfer::class, "transferable")
|
|
->where("status", StatusTypeEnum::APPROVED)
|
|
->orderBy("id", "desc");
|
|
}
|
|
|
|
public function latestTransfer(): MorphOne
|
|
{
|
|
return $this->morphOne(Transfer::class, "transferable")
|
|
->where("status", StatusTypeEnum::APPROVED)
|
|
->orderBy("id", "desc")->latest();
|
|
}
|
|
|
|
public function transferActive(): MorphMany
|
|
{
|
|
return $this->morphMany(Transfer::class, "transferable")
|
|
->where("status", StatusTypeEnum::APPROVED)
|
|
->whereNull("to_date")
|
|
->orderBy("id", "desc");
|
|
}
|
|
|
|
public function registerMediaCollections(): void
|
|
{
|
|
$this->addMediaCollection('files')
|
|
->useFallbackUrl('/files/default_pastry.jpg')
|
|
->useFallbackPath(public_path('/files/default_pastry.jpg'));
|
|
}
|
|
|
|
/**
|
|
* @throws InvalidManipulation
|
|
*/
|
|
public function registerMediaConversions(Media $media = null): void
|
|
{
|
|
$this->addMediaConversion('thumbnail')
|
|
->width(480)
|
|
->height(640)
|
|
->nonQueued()
|
|
->performOnCollections('files');
|
|
}
|
|
|
|
protected static function getValidCollections(): array
|
|
{
|
|
return [
|
|
'files'
|
|
];
|
|
}
|
|
}
|