Laravel 需要意外的多对多关系 table 名称
Laravel many-to-many relation required unexpected table name
我有两个 table 具有多对多关系
class Psychologist extends Model
{
public function specs()
{
return $this->belongsToMany('App\Models\Spec');
}
public function methods()
{
return $this->belongsToMany('App\Models\Method');
}
}
它在使用 table 名称 'psychologist_spec' 保存第一个关系 'App\Models\Spec' $psy->specs()->attach(explode(',',$data['spec']));
时工作正常,并得到不正确 table 'method_psychologist' 的异常
而不是 'psychologist_method'
public function save(Request $req)
{
$data = $req->all();
$psy = Psychologist::create($data);
$psy->specs()->attach(explode(',',$data['spec']));
$psy->methods()->attach(explode(',',$data['methods']));
}
我知道我可以明确指出 table 名字,例如
public function methods()
{
return $this->belongsToMany('App\Models\Method', 'psychologist_method');
}
但无法理解为什么它在第一种情况下有效而在第二种情况下却不起作用
Eloquent 将按字母顺序加入两个相关的模型名称
我有两个 table 具有多对多关系
class Psychologist extends Model
{
public function specs()
{
return $this->belongsToMany('App\Models\Spec');
}
public function methods()
{
return $this->belongsToMany('App\Models\Method');
}
}
它在使用 table 名称 'psychologist_spec' 保存第一个关系 'App\Models\Spec' $psy->specs()->attach(explode(',',$data['spec']));
时工作正常,并得到不正确 table 'method_psychologist' 的异常
而不是 'psychologist_method'
public function save(Request $req)
{
$data = $req->all();
$psy = Psychologist::create($data);
$psy->specs()->attach(explode(',',$data['spec']));
$psy->methods()->attach(explode(',',$data['methods']));
}
我知道我可以明确指出 table 名字,例如
public function methods()
{
return $this->belongsToMany('App\Models\Method', 'psychologist_method');
}
但无法理解为什么它在第一种情况下有效而在第二种情况下却不起作用
Eloquent 将按字母顺序加入两个相关的模型名称