2016-07-21 95 views
0

我有兩個models名稱PluginsUser與表分別。我試圖在名爲create_users_plugins_table.phpmigration中命名爲,其中pivot table的名稱爲plugin_userLaravel多對多關係錯誤

以下是架構:

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

User model我下面的關係功能:

public function userplugins(){ 

    return $this->belongsToMany('App\Plugins')->withPivot('contents'); 
} 

雖然通過下面的代碼讀取行:

$user = User::findorFail($id); 
return $user->userplugins()->first()->contents; 

我會出現以下錯誤:

SQLSTATE[42S02]: Base table or view not found: 1146 Table 'nitsbuilder.plugins_user' doesn't exist (SQL: select plugins .*, plugins_user . user_id as pivot_user_id , plugins_user . plugins_id as pivot_plugins_id , plugins_user . contents as pivot_contents from plugins inner join plugins_user on plugins . id = plugins_user . plugins_id where plugins_user . user_id = 1 limit 1)

請幫我解決這個錯誤。

感謝

+0

希望你已經運行你的遷移?你的'Plugins'模型中的代碼是什麼? –

+1

就這麼簡單:plugin_user!== plugins_user錯誤正好告訴你這個問題。您沒有定義plugins_user,而是plugin_user - 您可以在您的關係中更改被引用的表名稱https://laravel.com/docs/5.1/eloquent-relationships#many-to-many –

+0

@ArifulHaque我已經這樣做了。 –

回答

1

看起來像你的移民,你的表名是

`plugin_user` 

但查詢搜索

`plugins_user` 

您可能需要使用表名更新您的Plugin模型。