Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
ProductCollection
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 products
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 booted
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3namespace App\Models;
4
5use App\Models\admin\Product;
6use App\Service\CacheService;
7use Illuminate\Database\Eloquent\Factories\HasFactory;
8use Illuminate\Database\Eloquent\Model;
9use Illuminate\Support\Str;
10
11class ProductCollection extends Model
12{
13    use HasFactory;
14
15    protected $fillable = ['name', 'slug', 'description', 'image', 'meta_title', 'meta_description', 'is_active', 'sort_order'];
16    protected $casts = ['is_active' => 'boolean', 'sort_order' => 'integer'];
17
18    public function products()
19    {
20        return $this->belongsToMany(Product::class, 'product_collection_product')
21            ->withPivot('sort_order')->orderByPivot('sort_order');
22    }
23
24    protected static function booted(): void
25    {
26        static::saving(fn (self $collection) => $collection->slug = Str::slug($collection->slug ?: $collection->name));
27        static::saved(fn () => CacheService::forgetWidgets());
28        static::deleted(fn () => CacheService::forgetWidgets());
29    }
30}