I have two tables with many-to-many relation
class Psychologist extends Model
{
public function specs()
{
return $this->belongsToMany('App\Models\Spec');
}
public function methods()
{
return $this->belongsToMany('App\Models\Method');
}
}
Its working fine on saving first relation 'App\Models\Spec' $psy->specs()->attach(explode(',',$data['spec']));
with table name 'psychologist_spec' and get exception of incorrect table 'method_psychologist'
instead of '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']));
}
I know that i can explicitly indicate table name like
public function methods()
{
return $this->belongsToMany('App\Models\Method', 'psychologist_method');
}
but cant understand why it working in the first case and doesnt in the second
Eloquent will join the two related model names in alphabetical order