【问题标题】:SQLSTATE[42S02]: Base table or view not found (belongsToMany) Laravel 5.4SQLSTATE [42S02]:未找到基表或视图(belongsToMany)Laravel 5.4
【发布时间】:2018-01-15 18:09:07
【问题描述】:

请有人帮我解决这个问题。我是 laravel 的新手,现在我需要将一个数组从选择标签存储到 mysql,我遇到了这个错误。 SQLSTATE[42S02]:找不到基表或视图:1146 表“cmsystem.account_code_project”不存在(SQL:从account_code_project 中选择account_code_ac_id,其中project_pj_id = 1)。似乎它与使用 belongsToMany() 函数有关。我应该在这里做的是我的代码。

这是 project_table

 //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');
    });   

项目负责人

//ProjectController
   public function store(Request $request)
    {
    $this->validate($request,[
        'pj_title'=>'required',
        'pj_description'=>'required',
        ]);

    $project = new project;
    $project->pj_title = $request->pj_title;
    $project->pj_description = $request->pj_description;
    $project->save();

    $project->accounts()->sync($request->accounts, false);
    return redirect('/projectlist'); 
    }

型号

//AccountCode
  protected $table = 'accountcodes';
     public function projects()
{
    return $this->belongsToMany('App\Project');
}

//Project
  protected $table = 'projects';
    public function accounts()
{
    return $this->belongsToMany('App\AccountCode');
}

谢谢。

【问题讨论】:

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


    【解决方案1】:

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

    //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');
    }
    

    【讨论】:

      【解决方案2】:

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

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

      否则,Eloquent 会尝试按照其约定检测默认表名,我鼓励您遵循。

      【讨论】:

      • 我应该在我的两个模型上都指定它吗?我的意思是这样 return $this->belongsToMany('App\Project', 'code_project'); return $this->belongsToMany('App\AccountCode', 'code_project');
      • 在 belongsToMany 标签上添加“code_project”并没有改变任何东西,所以我所做的就是将表名更改为“accountcode_project”。而且错误仍然是一样的。我没有看到任何错误。我该怎么办?抱歉,谢谢
      【解决方案3】:

      您可以在belongsToMany中提示表名

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

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

      【讨论】:

      • 我应该在我的两个模型上都指定它吗?我的意思是这样 return $this->belongsToMany('App\Project', 'code_project'); return $this->belongsToMany('App\AccountCode', 'code_project');
      • @ClaudeDizon 是的,无论您需要在何处使用该数据透视表。问题是您没有遵循命名约定。
      • 在 belongsToMany 标签上添加“code_project”并没有改变任何东西,所以我所做的就是将表名更改为“accountcode_project”。而且错误仍然是一样的。我没有看到任何错误。我该怎么办?抱歉,谢谢。
      • @kalawadei Laravel 将假设如果您的模型名称是 AccountCode 那么您的表名称是 account_code 尝试将您的模型重命名为 Accoundcode(中间没有大写字母)看看是否有帮助
      猜你喜欢
      • 2017-04-19
      • 1970-01-01
      • 1970-01-01
      • 2018-03-16
      • 2016-06-21
      • 2018-03-11
      • 2016-07-15
      • 2018-04-30
      • 1970-01-01
      相关资源
      最近更新 更多