Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

do73's avatar
Level 1

Issue with many-to-many relationship in Laravel Nova

Hi guys,

I'm currently working on a Laravel Nova project and I'm running into an issue when attaching a resource to another resource through a many-to-many relationship.

I have Item, License, Category and Company models / resources with the following relationships:

  • Item has a many-to-many relationship with License
  • Category has a one-to-many relationship with Item
  • Company has a one-to-many relationship with Item

What I want to achieve is to be able to create an Item, and attach one or more Licenses to it. I also want to be able to view a License, and attach one or more Items to it.

Database

items table

Schema::create('items', function (Blueprint $table) {
    $table->id();
    $table->bigInteger('category_id')->unsigned()->index();
    $table->bigInteger('company_id')->unsigned()->index();
    // other unrelated columns...
});

licenses table

Schema::create('licenses', function (Blueprint $table) {
    $table->id();
    // other unrelated columns...
});

item_license pivot table

Schema::create('item_license', function (Blueprint $table) {
    $table->bigInteger('item_id')->unsigned()->index();
    $table->foreign('item_id')->references('id')->on('items');

    $table->bigInteger('license_id')->unsigned()->index();
    $table->foreign('license_id')->references('id')->on('licenses');

    $table->primary(['item_id', 'license_id']);
});

categories table

Schema::create('categories', function (Blueprint $table) {
    $table->id();
    // other unrelated columns...
});

companies table

Schema::create('companies', function (Blueprint $table) {
    $table->id();
    // other unrelated columns...
});

Models

In the Item model, I have defined the relationships like this:

public function licenses()
{
    return $this->belongsToMany(License::class);
}

public function category()
{
    return $this->belongsTo(Category::class);
}

public function company()
{
    return $this->belongsTo(Company::class);
}

In the License model, I have defined the relationship like this:

public function items()
{
    return $this->belongsToMany(Item::class);
}

In the Category model, I have defined the relationship like this:

public function items()
{
    return $this->hasMany(Item::class);
}

In the Company model, I have defined the relationship like this:

public function items()
{
    return $this->hasMany(Item::class);
}

Nova resources

In the Item resource, I have defined the relationships like this:

BelongsToMany::make('Licenses'),

BelongsTo::make('Category')
    ->withoutTrashed()
    ->sortable(),
                
BelongsTo::make('Company')
    ->withoutTrashed()
    ->sortable(),

In the License resource, I have defined the relationship like this:

BelongsToMany::make('Items'),

In the Category resource, I have defined the relationship like this:

HasMany::make('Items'),

In the Company resource, I have defined the relationship like this:

HasMany::make('Items'),

The issue that I am facing is, when I attach a License to an Item and click save, I get the following error Flash message:

SQLSTATE[42S02]: Base table or view not found: 1146 Table '[database name].pivot' doesn't exist (SQL: select * from `pivot` where `id` is null limit 1)

When I check the database, I can see that the database record DID successfully get added to the 'item_license' pivot table.

When I refresh the page after this, I get the same error and am unable to use the application.

The only way to recover from this is to run php artisan migrate:fresh and re-seed the database...

Here comes the interesting part: When I remove the one-to-many relationships between Item and Category and Company, the SQL error does not occur and the many-to-many relation ship between Item and License starts working!

Does anyone understand what is happening? I feel like I did eveything correctly, is anyone familar with this issue? I would really appreciate some help.

0 likes
6 replies
bugsysha's avatar

Have you renamed something because the query that is executed is on the table named pivot?

do73's avatar
Level 1

@bugsysha I have not manually named anything pivot. This looks like a generic Laravel / Nova error, so I feel like I have misconfigured something in the Model or Resource.

bugsysha's avatar

@do73 I never had this situation. Have you searched your app directory for a pivot reference?

do73's avatar
Level 1

@bugsysha I checked and there are no references to pivot in my application besides some in Nova's app.js and vendor.js files, but those don't seem to be related.

do73's avatar
Level 1

Update:

I have discovered that the many-to-many relationship actually starts working without issues when I remove the one-to-many relationships with other resources. I have updated my post with the relevant code and information.

Thorazine's avatar

It's long ago, but the solution is to use an id on your pivot (item_licence) table. Either that or change the primary key in your application. You can check this by seeing what the query does. It requests an id, not your primary key.

Please or to participate in this conversation.