I'm having two models
under the name of Plugins
and User
with tables respectively. I'm trying to have many to many relationship
with a pivot table
named plugin_user
in the migration
named create_users_plugins_table.php
.
Following is the schema:
public function up()
{
Schema::create('plugin_user', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('plugins_id');
$table->json('contents');
$table->timestamps();
});
}
In the User model
I've following function for relationship:
public function userplugins(){
return $this->belongsToMany('App\Plugins')->withPivot('contents');
}
While fetching the row by following code:
$user = User::findorFail($id);
return $user->userplugins()->first()->contents;
I'm getting an following error:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'nitsbuilder.plugins_user' doesn't exist (SQL: select
plugins
.*,plugins_user
.user_id
aspivot_user_id
,plugins_user
.plugins_id
aspivot_plugins_id
,plugins_user
.contents
aspivot_contents
fromplugins
inner joinplugins_user
onplugins
.id
=plugins_user
.plugins_id
whereplugins_user
.user_id
= 1 limit 1)
Please help me out in fixing this bug.
Thanks
Looks like in your Migration, your table name is
`plugin_user`
but the query searching for
`plugins_user`
You might need to update your Plugin
model with the table name.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments