Php 删除Laravel 5.8中的子类别

Php 删除Laravel 5.8中的子类别,php,laravel,laravel-5,Php,Laravel,Laravel 5,我是拉拉维尔的初学者。我在我的项目中使用了Laravel 5.8 我有以下代码: 型号: class ProductsCategory extends Model { protected $quarded = []; protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'level', 'parent_id', 'number']; public $timestamps = true;

我是拉拉维尔的初学者。我在我的项目中使用了Laravel 5.8

我有以下代码:

型号:

class ProductsCategory extends Model
{
    protected $quarded = [];
    protected $fillable = ['company_id', 'enable', 'name', 'url_address', 'level', 'parent_id', 'number'];
    public $timestamps = true;
    protected $table = 'products_category';
}
迁移:

public function up()
    {
        Schema::create('products_category', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->bigInteger('company_id')->unsigned();
            $table->foreign('company_id')->references('id')->on('companies')->onDelete('cascade');
            $table->char('enable', 1)->default(0);
            $table->string('name', 85)->nullable();
            $table->string('url_address', 160);
            $table->integer('level')->default(0);
            $table->bigInteger('parent_id')->default(0);
            $table->bigInteger('number')->default(0);
            $table->timestamps();
            $table->engine = "InnoDB";
            $table->charset = 'utf8mb4';
            $table->collation = 'utf8mb4_unicode_ci';
        });
    }
我在数据库中有许多类别和子类别。 子类别和类别通过父类id连接


如何删除记录以删除其所有子类别?

您可以在数据库中添加关系,并设置删除父类别时要删除的子类别

在您的迁移中:

$table->foreign('parent_id')->references('id')->on('products_category')->onDelete('cascade');

更新:您应该删除
->默认值(0)来自
parent\u id
防御。我不希望您有id为
0
的类别。只需通过添加
->nullable()使列为null标签

更新:laravel中的所有增量(
增量
大增量
,…)都是大小不同的无符号整数。 因此,您需要声明一个
unsignedbigigger
,使其与您正在引用的主键具有相同的类型

$table->unsignedBigInteger('parent_id')->nullable();
就像你对
company\u id

$table->bigInteger('company_id')->unsigned();

只需以相同的方式设置company_id上的外键,但将其引用到此行中products_类别的主键,并将默认值0更改为nullable,这样就不会中断关系

$table->bigInteger('parent_id')->nullable();
$table->foreign('parent_id')->references('id')->on('products_category')->onDelete('cascade');

我编写了以下代码:Schema::create('products_category',function(Blueprint$table){$table->bigingress('id');$table->biginger('company_id')->unsigned();$table->foreign('company_id')->references('id')->on('companys')->onDelete('cascade');……$table->biginger('parent_id')->默认值(0);$table->foreign('parent_id')->references('id')->on('products_category')->onDelete('cascade'));但我有一个错误:illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table
products\u category
add constraint
products\u category\u parent\u id\u foreign
外键(
parent\u id
)引用
products\u category
)在delete cascade)上,由于表中不存在0 id,您应该将其设置为可空,而不是默认值0,因为它会破坏任何关系。这没有帮助。我的全部代码:是的,我有。这是我的完整代码:。我有错误:illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table
products\u category
add constraint
products\u category\u parent\u id\u foreign
外键(
parent\u id
)引用
products\u category
)在delete cascade上)实际上,您确实需要理解数据库关系的概念,这是您唯一缺少的一个错误:illumb\database\QueryException:SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table
products\u category
add constraint
products\u category\u parent\u id\u foreign
外键(
parent\u id
)引用
products\u category
(删除级联上的
id
)我有:$table->biginger('parent\u id')->nullable();$table->foreign('parent\u id')->引用('id')->('products_category')->onDelete('cascade');-和错误:Illumb\Database\QueryException:SQLSTATE[HY000]:一般错误:1215无法添加外键约束(SQL:alter table
products_category
添加约束
products_category_parent_id_foreign
外键(
parent_id
)引用
products\u category
id
)在删除级联中)您的表中是否已经有任何
parent\u id
s在categories表中不存在?将
$table->biginger('parent\u id')->nullable();
更改为
$table->biginsignedinteger('parent\u id')->nullable();
所有增量字段类型均为unsigned@goslscs很抱歉,该方法实际上是
unsignedbiginger
您可以在这里检查它,您还可以在该链接上找到增量是无符号的