如何在laravel中的表内创建关系?

问题描述

我需要在表中创建一个关系。我在下面附上了我的桌子。

enter image description here

这是我的类别模型。

class Category extends Model
{
    public function products()
    {
       return $this->hasMany(Product::class);
    }

    public function categories_id() {
        return $this->hasMany('category_id','parent_id');
    }
    public function parent_id() {
        return $this->hasMany('category_id','parent_id');
    }
}

在这里如何关联category_id和parent_id?

这是我的 categories_table

 public function up()
    {
       Schema::create('categories',function (Blueprint $table)
        {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('parent_id')->nullable();
        $table->string('cat_name')->nullable();
        $table->string('cat_image_path')->nullable();
        $table->timestamps();
         });
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)