Spaces:
Running
Running
File size: 1,868 Bytes
40dca3b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 | <?php
namespace App\Models;
use App\Services\ContentImageServices;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\Sluggable\HasSlug;
use Spatie\Sluggable\SlugOptions;
class Chapter extends BaseModel
{
use HasSlug;
protected $table = "chapters";
protected $fillable =[
'prv_chapter_id',
'next_chapter_id',
'comic_id',
'status',
'chapter_name',
'chapter_number',
'publish_at',
'free_at',
'link_small_icon',
'link_small_icon_backup',
'slug',
'created_by',
'updated_by',
'created_at',
'updated_at',
];
const TIME = [
'free_at',
'publish_at',
];
public static function boot()
{
parent::boot();
static::deleting(function ($model) {
$model->load('contentImages');
$contentImageServices = app()->make(ContentImageServices::class);
if(isset($model->contentImages)){
foreach ($model->contentImages as $contentImage){
$contentImageServices->delete($contentImage->id);
}
}
return true;
});
}
public function getSlugOptions() : SlugOptions
{
return SlugOptions::create()
->generateSlugsFrom('chapter_name')
->saveSlugsTo('slug');
}
public function comic(){
return $this->belongsTo(Comic::class,'comic_id');
}
public function contentImages(){
return $this->hasMany(ContentImage::class,"chapter_id",'id');
}
public function nextChapter(){
return $this->belongsTo(Chapter::class,"next_chapter_id");
}
public function prvChapter(){
return $this->belongsTo(Chapter::class,"prv_chapter_id");
}
}
|