【发布时间】: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