mercredi 27 juillet 2016

laravel 5 polymorphic triples data in my database table

i don't know why does attching polymorphic models is inserting 3 rows in my DB table. for example:

$user = User::find($id);

$plantel = Plantel::find(1);

$privilegio = Privilegio::find(1);

$plantel->privilegios()->attach($privilegio->id); //tripling the info here
$plantel->usuarios()->attach($user->id);

my models. User model.

public function planteles(){
  return $this->morphToMany('App\Plantel', 'privilegio_ref', 'privilegios_usuarios')->withPivot('descripcion', 'url', 'pathImage', 'activo');      
}

Plantel model (Campus)

public function usuarios(){
  return $this->morphedByMany('App\User', 'privilegio_ref', 'privilegios_usuarios')->withPivot('descripcion', 'url', 'pathImage', 'activo');
}

public function privilegios(){
  return $this->morphedByMany('App\Privilegio', 'privilegio_ref', 'privilegios_usuarios')->withPivot('descripcion', 'url', 'pathImage', 'activo');
}

Privilegio Model (Privilege)

public function planteles(){
  return $this->morphToMany('App\Plantel', 'privilegio_ref', 'privilegios_usuarios')->withPivot('descripcion', 'url', 'pathImage', 'activo');      
}

this is my table with data triplicated



from Newest questions tagged laravel-5 - Stack Overflow http://ift.tt/2a0wJ6i
via IFTTT

Aucun commentaire:

Enregistrer un commentaire