Have you renamed something because the query that is executed is on the table named pivot?
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.
Please or to participate in this conversation.