即时通讯与我的应用程序有错误,它看起来一切都很好,但是创建了一个错误的查询,但我有我的表和模型正确,基本上我有一个用户的注册,可以选择多个地区和行业/区域。我的错误是:
SQLSTATE[42S02]: Base table or view not found: 1146 Table 'hjobs.region_user' doesn't exist (SQL: select `region_id` from `region_user` where `user_id` is null)
模特用户:
public function regionsToWork(){
return $this->belongsToMany(Region::class);
}
public function industriesToWork(){
return $this->belongsToMany(Industry::class);
}
AuthController:
$user = new User();
$user->name = $data['name'];
...
$user->regionsToWork()->sync($data['region'], false);
$user->industriesToWork()->sync($data['industry'], false);
$user->save();
Tables DB: USER:
-id;
-email
...
user_industry:
id;
user_id;
industry_id;
user_region:
id;
user_id;
region_id;
迁移user_region_table_creation
Schema::create('user_region', function($table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('region_id')->unsigned();
$table->foreign('region_id')->references('id')->on('regions');
});
user_industry_table_creation
Schema::create('user_industry', function($table){
$table->increments('id');
$table->integer('user_id')->unsigned();
$table->foreign('user_id')->references('id')->on('users');
$table->integer('industry_id')->unsigned();
$table->foreign('industry_id')->references('id')->on('industries');
});
答案 0 :(得分:1)
看起来数据透视表名称与预期的不同,是region_user而不是user_region。
您可以在belongsToMany方法中手动指定数据透视表名称,如下所示:
return $this->belongsToMany(Region::class, 'user_region');