55 lines
1.4 KiB
PHP
55 lines
1.4 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use App\Models\User;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
|
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
|
use Illuminate\Database\Eloquent\Relations\MorphToMany;
|
|
|
|
class Memo extends Model
|
|
{
|
|
protected $guarded = [];
|
|
|
|
public function path()
|
|
{
|
|
return '/memos/' . $this->id;
|
|
}
|
|
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class);
|
|
}
|
|
|
|
// public function tags(): MorphToMany
|
|
// {
|
|
// return $this->morphToMany(Tag::class, 'taggable')->withTimestamps()->withPivot('user_id');
|
|
// }
|
|
|
|
public function images(): MorphMany
|
|
{
|
|
return $this->morphMany(Image::class, 'imageable');
|
|
}
|
|
|
|
public function coverImage(): MorphOne
|
|
{
|
|
return $this->morphOne(Image::class, 'imageable')
|
|
->orderBy('id', 'desc')
|
|
->where('location', 'cover')
|
|
->withDefault(function ($userImage) {
|
|
$userImage->path = 'images/default-cover.jpg';
|
|
});
|
|
}
|
|
|
|
public function thumbnailImage(): MorphOne
|
|
{
|
|
return $this->morphOne(Image::class, 'imageable')
|
|
->orderBy('id', 'desc')
|
|
->where('location', 'cover-small')
|
|
->withDefault(function ($userImage) {
|
|
$userImage->path = 'images/default-cover.jpg';
|
|
});
|
|
}
|
|
}
|