86 lines
2.2 KiB
PHP
86 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Traits\Models\ActivityLog;
|
|
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
|
|
/**
|
|
* @property string $title Video title.
|
|
* @property string $video_id Youtube/Vimeo video ID.
|
|
* @property \App\Models\Product|null $product_id Associated product.
|
|
* @property integer $order Change order request.
|
|
* @property boolean $published Video publish status.
|
|
* @property boolean $is_on_homepage Is the video on the home page?
|
|
* @property-read \Illuminate\Support\Carbon $created_at Created date.
|
|
* @property-read \Illuminate\Support\Carbon $updated_at Updated date.
|
|
*
|
|
* @property-read \App\Models\Product $product Product associated to the video. .
|
|
*/
|
|
class Video extends Model
|
|
{
|
|
use ActivityLog;
|
|
use HasFactory;
|
|
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array<int, string>
|
|
*/
|
|
protected $fillable = [
|
|
'title',
|
|
'video_id',
|
|
'product_id',
|
|
'is_on_homepage',
|
|
'order',
|
|
'published',
|
|
];
|
|
|
|
/**
|
|
* The attributes that should be cast.
|
|
*
|
|
* @var array<string, string>
|
|
*/
|
|
protected $casts = [
|
|
'published' => 'boolean',
|
|
'is_on_homepage' => 'boolean',
|
|
];
|
|
|
|
/**
|
|
* Perform any actions required after the model boots.
|
|
*
|
|
* @return void
|
|
*/
|
|
protected static function booted(): void
|
|
{
|
|
static::creating(function (self $video) {
|
|
self::setOrder($video);
|
|
});
|
|
}
|
|
|
|
// * METHODS
|
|
|
|
/**
|
|
* Set order after the last element of the list.
|
|
*
|
|
* @param self $model
|
|
* @return void
|
|
*/
|
|
private static function setOrder(self $model): void
|
|
{
|
|
$model->order = \intval(self::query()->max('order')) + 1;
|
|
}
|
|
|
|
// * RELATIONSHIPS
|
|
|
|
/**
|
|
* Get the product for the video.
|
|
*
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
*/
|
|
public function product(): \Illuminate\Database\Eloquent\Relations\BelongsTo
|
|
{
|
|
return $this->belongsTo(Product::class);
|
|
}
|
|
}
|