lsky-pro/app/Models/Album.php
2022-02-20 16:45:32 +08:00

71 lines
1.8 KiB
PHP

<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\HasMany;
use Illuminate\Http\Request;
/**
* @property int $id
* @property int $user_id
* @property string $name
* @property string $intro
* @property int $image_num
* @property-read User $user
* @property-read Collection $images
*/
class Album extends Model
{
use HasFactory;
protected $fillable = [
'name',
'intro',
'image_num'
];
protected $hidden = [
'user_id',
];
protected $attributes = [
'intro' => '',
];
public function scopeFilter(Builder $builder, Request $request)
{
return $builder->when($request->query('order') ?: 'newest', function (Builder $builder, $order) {
switch ($order) {
case 'earliest':
$builder->orderBy('created_at');
break;
case 'most':
$builder->orderByDesc('image_num');
break;
case 'least':
$builder->orderBy('image_num');
break;
default:
$builder->latest();
}
})->when($request->query('keyword'), function (Builder $builder, $keyword) {
$builder->whereRaw("concat(name,intro) like ?", ["%{$keyword}%"]);
});
}
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id', 'id');
}
public function images(): HasMany
{
return $this->hasMany(Image::class, 'album_id', 'id');
}
}