Please someone help me on this. I am new to laravel and now I need to store an array from a select tag to mysql and I am having this erorr.
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'cmsystem.account_code_project' doesn't exist (SQL: select account_code_ac_id
from account_code_project
where project_pj_id
= 1). Seems like it has something to do with using belongsToMany() fucntion. What should I do here is my code.
here's the project_table
//project table
Schema::create('projects', function(Blueprint $table)
{
$table->increments('pj_id');
$table->string('pj_title',100);
$table->string('pj_description');
});
here's the accountcodes table
//codes
Schema::create('accountcodes', function(Blueprint $table)
{
$table->increments('ac_id');
$table->string('ac_code',100)->nullable();
$table->string('ac_description')->nullable();
});
here's the intermediate table for the two tables
//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');
});
Project Controller
//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');
}
Models
//AccountCode
protected $table = 'accountcodes';
public function projects()
{
return $this->belongsToMany('App\Project');
}
//Project
protected $table = 'projects';
public function accounts()
{
return $this->belongsToMany('App\AccountCode');
}
Thank you.