我的数据库中有3个表,categories
,subcategories
和childcategories
。
子类别在删除时具有级联,因此每次我删除类别时,子类别也会被删除。子类别也一样。
我的问题是,他们所有人都可以拥有attributes
,当我向子类别添加属性并删除类别时,他会删除所有内容,但不要删除子类别的属性。
我已经尝试了雄辩的事件,一些在github ..中找到的软件包,但是没人管。
类别模型:
class Category extends LocalizedModel {
public $translatedAttributes = ['name'];
protected $fillable = ['slug','photo','is_featured','image'];
public $timestamps = false;
public function subs()
{
return $this->hasMany('App\Models\Subcategory')->orderByTranslation('name', 'asc')->where('status','=',1);
}
public function attributes() {
return $this->morphMany('App\Models\Attribute', 'attributable');
}
子类别模型:
class Subcategory extends LocalizedModel {
public $translatedAttributes = ['name'];
protected $fillable = ['category_id','slug'];
public $timestamps = false;
public function childs()
{
return $this->hasMany('App\Models\Childcategory')->where('status','=',1);
}
public function category()
{
return $this->belongsTo('App\Models\Category')->withDefault(function ($data) {
foreach($data->getFillable() as $dt){
$data[$dt] = __('Deleted');
}
});
}
public function attributes() {
return $this->morphMany('App\Models\Attribute', 'attributable');
}
}
子类别模型:
class Childcategory extends LocalizedModel{
protected $cascadeDeletes = ['attributes'];
public $translatedAttributes = ['name'];
protected $fillable = ['subcategory_id','slug'];
public $timestamps = false;
public function subcategory()
{
return $this->belongsTo('App\Models\Subcategory')->withDefault(function ($data) {
foreach($data->getFillable() as $dt){
$data[$dt] = __('Deleted');
}
});
}
public function products()
{
return $this->hasMany('App\Models\Product');
}
public function setSlugAttribute($value)
{
$this->attributes['slug'] = str_replace(' ', '-', $value);
}
public function attributes() {
return $this->morphMany('App\Models\Attribute', 'attributable');
}
}