Advertisement
Guest User

usando metodo sync

a guest
Jul 16th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.25 KB | None | 0 0
  1. class Funcao extends Model
  2. {
  3.     protected $table = 'funcao';
  4.     protected $primaryKey = 'id';
  5.     protected $fillable = ['id','url','nome'];
  6.     public $timestamps = false;
  7.  
  8.     public function areas()
  9.     {
  10.         return $this->belongsToMany(area_atuacao::class,'fun_area','fun_id','area_id')
  11.             ->as('fun_area')
  12.             ->withPivot(['mostrarMenu']);
  13.     }
  14.  
  15. }
  16.  
  17. class area_atuacao extends Model
  18. {
  19.     //
  20.     protected $primaryKey = 'id';
  21.     public $timestamps = false;
  22.     protected $fillable = ['id','nome','ativo'];
  23.  
  24.     public function funcaos()
  25.     {
  26.         return $this->belongsToMany(Funcao::class,'fun_area','area_id','fun_id')
  27.             ->as('fun_area')
  28.             ->withPivot(['mostrarMenu']);
  29.     }
  30. }
  31.  
  32. //controller
  33.     public function store(Request $request)
  34.     {
  35.         //
  36.         if(!empty($request['permissoes']))
  37.         {
  38.             $funcao = null;
  39.             foreach ($request['permissoes'] as $permissoe)
  40.             {
  41.                 $idFuncao = (integer)$permissoe["funcao"];
  42.  
  43.                 $funcao = new Funcao();
  44.                 $funcao = $funcao->find($idFuncao);
  45.                 $funcao->areas()->sync(['area_id'=>$permissoe['area'],'mostrarMenu'=>false]);
  46.             }
  47.         }
  48.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement