I have a model SalesArea
which hasMany Regions
. Regions
belongTo one SalesArea
.
class SalesArea extends Model
{
public function regions() {
return $this->hasMany('App\Region');
}
}
class Region extends Model
{
public function sales_area() {
return $this->belongsTo('App\SalesArea');
}
}
Due to some changes in organization, the parent (SalesArea) is being created after the Regions have been created. I am trying to associate the Regions with their associated Sales areas when the Sales Area record is created.
I created a migration to add sales_area_id to the regions
table, and they are currently all null.
I have a select box that has the various IDs of Regions and I would like to associate the regions with a Sales Area at once with an array of ids. None of the association methods are working.
$area = SalesArea::create($request->all());
$area->regions()->attach([1,2,3]); // BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::attach()
$area->regions()->add([1,2,3]); // BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::add()'
$area->regions()->sync([1,2,3]); // BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::sync()'
$area->regions()->associate([1,2,3]); // BadMethodCallException with message 'Call to undefined method Illuminate\Database\Query\Builder::associate()'
I am stumped on how this is supposed to work. Regions already exist, I just need to add their relationships to their associated sales area.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire