TheOnlineThomas
1 month ago

Laravel Many to many understanding issue in Nova

Posted 1 month ago by TheOnlineThomas

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.

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');
        });

Please sign in or create an account to participate in this conversation.