-1
我創建一個遷移如下:組關係中laravel
Category::create([
'id' => 1,
'parent' => null,
'category_name' => "root"
]);
Category::create([
'id' => 2,
'parent' => 1,
'category_name' => "something"
]);
Category::create([
'id' => 3,
'parent' => 2,
'category_name' => "something"
]);
Category::create([
'id' => 4,
'parent' => 2,
'category_name' => "something"
]);
and etc ...
也這是我的模型,它是自引用:
public function up()
{
//
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->unsignedInteger('parent')->nullable()->unsigned();;
$table->string('category_name');
$table->text('category_lable');
$table->timestamps();
});
Schema::table('categories', function (Blueprint $table) {
$table->foreign('parent')
->references('id')->on('categories')
->onDelete('cascade')
->onUpdate('cascade');
}) ;
}
和遷移是通過這種方式接種:
class Category extends Model { public function getchildren() { return $this->has_many('App\Category' , 'parent'); } public function getparent() { return $this->belongs_to('App\Category' , 'parent'); }
}
在修補程序中,當我嘗試獲取某個節點父節點或子節點時,出現錯誤。
錯誤:
BadMethodCallException與消息 '調用未定義方法照亮\數據庫\查詢\生成器::的getParent()'
或
BadMethodCallException與消息「調用未定義的方法Illuminate \ Database \ Query \ Builder :: getchildren()'
我的錯在哪裏?
tnanks @ jan-willem請留意。這裏是我的:$ cat = Category :: findOrFail(26)然後:$ cat-> children() – user3634011
查看我更新的答案。 –
它返回null:((($ model-> children) – user3634011