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



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire