Laravel Nova 多对多资源失败
Laravel Nova many-to-many resource failing
我有两个资源,Bricks 和 Walls。
Brick
模型定义为
class Brick extends Model
{
public function walls()
{
return $this->belongsToMany('App\Wall')->withTimestamps();
}
}
并且 Wall
模型定义为
class Wall extends Model
{
public function bricks()
{
return $this->hasMany('App\Brick');
}
}
想法是一堵墙可以有很多砖,一块砖可以属于很多墙。
在 Nova 中,我将 Wall
字段设置为
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
HasMany::make('Bricks')
];
}
并且 Brick
字段设置为
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
BelongsToMany::make('Walls')
];
}
当尝试通过砖块资源将砖块连接到墙上时,我收到错误 SQLSTATE[HY000]: General error: 1 no such table: main.wall_id (SQL: insert into "brick_wall" ("brick_id", "wall_id", "created_at", "updated_at") values (5, 1, 2018-11-18 22:08:23, 2018-11-18 22:08:23))
,而当尝试通过墙资源将砖块添加到墙上时,我收到 SQLSTATE[HY000]: General error: 1 no such column: bricks.wall_id (SQL: select * from "bricks" where "bricks"."wall_id" = 1 and "bricks"."wall_id" is not null)
我试过切换 belongsToMany
和 hasMany
关系,但没有用。
编辑:这是中间方案table
Schema::create('brick_wall', function (Blueprint $table) {
$table->increments('id');
$table->integer('brick_id')->unsigned();
$table->integer('wall_id')->unsigned();
$table->foreign('brick_id')->references('id')->on('brick_id');
$table->foreign('wall_id')->references('id')->on('wall_id');
});
对于多对多关系,你必须有中间 table,例如 mysql 数据库中的 brick_wall
,才能在 brick
之间建立多对多关系table 和 wall
table。它包含列 brick_id
和 wall_id
并且与 brick
table 和 wall
table 具有外键关系。
要定义多对多关系,两个模型都使用 Eloquent 中的 belongsToMany
函数。它将找到 brick
和 wall
table 之间的关系,其中 {tableName}_id
作为默认列名作为主键和外键。
如果使用非标准eloquent名称作为主键和外键,可以参考eloquentlaravel官方文档https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
我有两个资源,Bricks 和 Walls。
Brick
模型定义为
class Brick extends Model
{
public function walls()
{
return $this->belongsToMany('App\Wall')->withTimestamps();
}
}
并且 Wall
模型定义为
class Wall extends Model
{
public function bricks()
{
return $this->hasMany('App\Brick');
}
}
想法是一堵墙可以有很多砖,一块砖可以属于很多墙。
在 Nova 中,我将 Wall
字段设置为
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
HasMany::make('Bricks')
];
}
并且 Brick
字段设置为
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
BelongsToMany::make('Walls')
];
}
当尝试通过砖块资源将砖块连接到墙上时,我收到错误 SQLSTATE[HY000]: General error: 1 no such table: main.wall_id (SQL: insert into "brick_wall" ("brick_id", "wall_id", "created_at", "updated_at") values (5, 1, 2018-11-18 22:08:23, 2018-11-18 22:08:23))
,而当尝试通过墙资源将砖块添加到墙上时,我收到 SQLSTATE[HY000]: General error: 1 no such column: bricks.wall_id (SQL: select * from "bricks" where "bricks"."wall_id" = 1 and "bricks"."wall_id" is not null)
我试过切换 belongsToMany
和 hasMany
关系,但没有用。
编辑:这是中间方案table
Schema::create('brick_wall', function (Blueprint $table) {
$table->increments('id');
$table->integer('brick_id')->unsigned();
$table->integer('wall_id')->unsigned();
$table->foreign('brick_id')->references('id')->on('brick_id');
$table->foreign('wall_id')->references('id')->on('wall_id');
});
对于多对多关系,你必须有中间 table,例如 mysql 数据库中的 brick_wall
,才能在 brick
之间建立多对多关系table 和 wall
table。它包含列 brick_id
和 wall_id
并且与 brick
table 和 wall
table 具有外键关系。
要定义多对多关系,两个模型都使用 Eloquent 中的 belongsToMany
函数。它将找到 brick
和 wall
table 之间的关系,其中 {tableName}_id
作为默认列名作为主键和外键。
如果使用非标准eloquent名称作为主键和外键,可以参考eloquentlaravel官方文档https://laravel.com/docs/5.7/eloquent-relationships#many-to-many