mercredi 17 avril 2019

Laravel 5.0 sync() on relation not working when storing

I have a model Mailgroup and a model CommunicationType.

I Mailgroup can have multiple CommuniationType's. This is my relation:

Mailgroup model:

public function communicationTypes()
{
    return $this->hasMany('App\CommunicationType');
}

CommunicationType model:

public function mailgroup()
{
    return $this->belongsTo('App\ImageRequest');
}

This the code I do to try and store a new Mailgroup.

$data = $this->request->all();
$mailgroup = new Mailgroup($data);
$mailgroup->communicationTypes()->sync($data['communication_types']);

Result of $data:

array:5 [▼
  "_token" => "j8lcEMggCakzANNbeVLYZttdOLUwJYKIJi0m85e6"
  "name" => "a"
  "administrator" => "abc"
  "communication_types" => array:2 [▼
    0 => "a"
    1 => "a"
  ]
  "site_id" => 4
]

The error:

Call to undefined method Illuminate\Database\Query\Builder::sync()

Am I doing something wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire