Open
Description
Hi,
I'm having a problem when I try to delete a node with descendants.
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Post\Category;
class Post extends Model
{
use HasFactory;
...
/**
* The categories that belong to the post.
*/
public function categories()
{
return $this->belongsToMany(Category::class);
}
...
}
namespace App\Models\Post;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Post;
use Kalnoy\Nestedset\NodeTrait;
class Category extends Model
{
use HasFactory, NodeTrait;
protected $table = 'post_categories';
...
/**
* The posts that belong to the category.
*/
public function posts()
{
return $this->belongsToMany(Post::class);
}
public function delete()
{
$this->posts()->detach();
parent::delete();
}
...
}
When I delete a node that hasn't any descendant everything it's ok.
$category->delete();
Now when I try to delete a node that has one or more descendants I get this error:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`myproject`.`category_post`, CONSTRAINT `category_post_category_id_foreign` FOREIGN KEY (`category_id`) REFERENCES `post_categories` (`id`))
delete from `post_categories` where `post_categories`.`_lft` between 4 and 8
It looks like the Category delete() method is not called when the nested-set trait deletes the descendants.
How can I fix this issue, should I delete the descendant nodes manually ?
Metadata
Metadata
Assignees
Labels
No labels