1
0
Fork 0
internet-everythings-agricu.../app/Models/Banner.php

48 lines
1.2 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Support\Str;
use EloquentFilter\Filterable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Storage;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Banner extends Model
{
use Filterable;
protected $fillable = ['is_enable', 'link_config', 'name', 'picture', 'place_id', 'published_at', 'sort'];
protected $casts = [
'created_at' => 'datetime:Y-m-d H:i:s',
'updated_at' => 'datetime:Y-m-d H:i:s',
'published_at' => 'datetime:Y-m-d H:i:s',
'is_enable' => 'boolean',
'link_config' => 'json',
];
protected function picture(): Attribute
{
return Attribute::make(
get: fn($value) => $value ? (Str::startsWith($value, ['http://', 'https://']) ? $value : Storage::url($value)) : '',
);
}
public function place()
{
return $this->belongsTo(BannerPlace::class, 'place_id');
}
public function scopeSort($q)
{
return $q->orderBy('sort', 'desc');
}
public function scopeEnable($q)
{
return $q->where('is_enable', 1)->where('published_at', '<=', now());
}
}