finish memos cover
This commit is contained in:
15
app/Models/Image.php
Normal file
15
app/Models/Image.php
Normal file
@@ -0,0 +1,15 @@
|
||||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Image extends Model
|
||||
{
|
||||
protected $guarded = [];
|
||||
|
||||
public function imageable()
|
||||
{
|
||||
return $this->morphTo();
|
||||
}
|
||||
}
|
||||
@@ -4,6 +4,8 @@ namespace App\Models;
|
||||
|
||||
use App\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
|
||||
@@ -24,4 +26,19 @@ class Memo extends Model
|
||||
// {
|
||||
// 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';
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace App;
|
||||
|
||||
use App\Models\Image;
|
||||
use App\Models\Memo;
|
||||
use Illuminate\Contracts\Auth\MustVerifyEmail;
|
||||
use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphMany;
|
||||
use Illuminate\Database\Eloquent\Relations\MorphOne;
|
||||
use Illuminate\Foundation\Auth\User as Authenticatable;
|
||||
use Illuminate\Notifications\Notifiable;
|
||||
use Laravel\Passport\HasApiTokens;
|
||||
@@ -54,4 +57,23 @@ class User extends Authenticatable
|
||||
{
|
||||
return $this->hasMany(Memo::class);
|
||||
}
|
||||
|
||||
public function images(): MorphMany
|
||||
{
|
||||
return $this->morphMany(Image::class, 'imageable');
|
||||
}
|
||||
|
||||
public function profileImage(): MorphOne
|
||||
{
|
||||
return $this->morphOne(Image::class, 'imageable')
|
||||
->where('location', 'profile')
|
||||
->orderBy('id', 'desc');
|
||||
}
|
||||
|
||||
public function coverImage(): MorphOne
|
||||
{
|
||||
return $this->morphOne(Image::class, 'imageable')
|
||||
->where('location', 'cover')
|
||||
->orderBy('id', 'desc');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user