SQLSTATE [42S02]:找不到基表或视图(belongsToMany)Laravel 5.4

时间:2017-08-08 08:54:56

标签: arrays laravel select has-and-belongs-to-many

请有人帮我解决这个问题。我是laravel的新手,现在我需要将一个数组从select标签存储到mysql,我有这个erorr。 SQLSTATE [42S02]:未找到基表或视图:1146表'cmsystem.account_code_project'不存在(SQL:从var actOnSelectCount = 2; var selectCount = 0; $scope.onSelectedFetchOther = function() { incrementAndActIfReady(); } $scope.onSelectedFetchDD = function(item) { incrementAndActIfReady(); } function incrementAndActIfReady = function() { selectCount++; if (selectCount === actOnSelectCount) { console.log("Doing something, 2 were selected!"); } } 中选择var actOnSelectCount = 2; var selected = []; $scope.onSelectedFetchOther = function() { incrementAndActIfReady('A'); } $scope.onSelectedFetchDD = function(item) { incrementAndActIfReady('B'); } function incrementAndActIfReady = function(selector) { // Push selection to selected if not // already there. else it was a change in same // select. if (selected.indexOf(selector) != -1) { selected.push(selector); } if (selection.length === actOnSelectCount) { console.log("Doing something, 2 were selected!"); } } ,其中account_code_ac_id = 1) 即可。似乎它与使用belongsToMany()函数有关。我这里应该做的是我的代码。

这是project_table

account_code_project

这是帐户代码表

project_pj_id

这是两个表的中间表

 //project table
       Schema::create('projects', function(Blueprint $table)
    {
        $table->increments('pj_id');
        $table->string('pj_title',100);
        $table->string('pj_description');
    });

项目控制器

 //codes
       Schema::create('accountcodes', function(Blueprint $table)
    {
        $table->increments('ac_id');
        $table->string('ac_code',100)->nullable();
        $table->string('ac_description')->nullable();
    });   

模型

//intermediate table
    Schema::create('code_project', function(Blueprint $table){
        $table->increments('id');
        $table->integer('project_id')->unsigned();
        $table->foreign('project_id')->references('pj_id')->on('projects');

        $table->integer('account_id')->unsigned();
        $table->foreign('account_id')->references('ac_id')->on('accountcodes');
    });   

谢谢。

3 个答案:

答案 0 :(得分:1)

您可以在belongsToMany

中提示表名
return $this->belongsToMany('App\AccountCode',"code_project");

这是因为按照惯例,您的数据透视表应该已经命名为:accountcode_project(它也不能反映您的模型名称应该是Accountcode

答案 1 :(得分:1)

这是因为您必须在模型关系中指定数据透视表名称:

return $this->belongsToMany('App\Project', 'code_project');

否则Eloquent会尝试按照惯例检测默认表名,我建议您遵循这些名称。

答案 2 :(得分:0)

好吧,我通过声明两个模型的代表来解决它,所以它看起来像这个

//AccountCode
      public function projects()
{
    return $this->belongsToMany('App\Project', 'accountcode_project', 'ac_id', 'pj_id');
}


// Project
    public function accountcodes()
{
    return $this->belongsToMany('App\AccountCode', 'accountcode_project', 'pj_id', 'ac_id');
}