[英]Creating records with relations between multiple regions and industries
im having a error with my app, it looks averything fine, but is creating a wrong query, but i have my tables and models right, basically i have a registration of a user that can select more than one regions and industry/area. 即时通讯在我的应用程序中出现错误,看起来一切正常,但是创建了错误的查询,但是我的表和模型正确,基本上,我注册的用户可以选择多个地区和行业/领域。 My error is:
我的错误是:
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)
Model User: 模型用户:
public function regionsToWork(){
return $this->belongsToMany(Region::class);
}
public function industriesToWork(){
return $this->belongsToMany(Industry::class);
}
AuthController: 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;
Migrations user_region_table_creation 迁移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 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');
});
It looks like the pivot table name is different than what it's expecting, region_user as opposed to user_region. 数据透视表名称似乎与期望的名称不同,region_user与user_region相反。
You can manually specify the pivot table name in your belongsToMany method like this: 您可以像下面这样在您的belongsToMany方法中手动指定数据透视表名称:
return $this->belongsToMany(Region::class, 'user_region');
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.