Laravel Nova Many-to-many Resource Failing
I've got two resources, Bricks and Walls.
The Brick
model is defined as
class Brick extends Model
{
public function walls()
{
return $this->belongsToMany('App\Wall')->withTimestamps();
}
}
and the Wall
model is defined as
class Wall extends Model
{
public function bricks()
{
return $this->hasMany('App\Brick');
}
}
The idea being that a wall can have many bricks, and a brick can belong to many walls.
In Nova, I've got the Wall
fields set up as
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
HasMany::make('Bricks')
];
}
and the Brick
fields set up as
public function fields(Request $request)
{
return [
ID::make()->sortable(),
Text::make('Name'),
BelongsToMany::make('Walls')
];
}
When trying to attach a brick to a wall through the brick resource, I get the error 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))
, and when trying to add a brick to a wall through the wall resource I get 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)
I've tried switching the belongsToMany
and hasMany
relationships but it doesn't help.
Edit: Here's the scheme for the intermediate 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');
});
Answer
For many to many relationship, you have to had intermediate table, for example brick_wall
in your mysql database, to establish many to many relationship between brick
table and wall
table. It contains column brick_id
and wall_id
and having foreign key relations to brick
table and wall
table.
To define many to many relationship, both model use belongsToMany
function from Eloquent. It will find relationship between brick
and wall
table with {tableName}_id
as default column name as primary key and foreign.
If you are using non standard eloquent name as primary key and foreign key, you can refer to eloquent laravel official documentation https://laravel.com/docs/5.7/eloquent-relationships#many-to-many
Related Questions
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS - Conditionally Load a Different Page
- → Make a Laravel collection into angular array (octobercms)
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?
- → Validating fileupload(image Dimensions) in Backend Octobercms
- → OctoberCMS Fileupload completely destroys my backend
- → How do I call the value from another backed page form and use it on a component in OctoberCms