How to filter Combobox data using the Laravel Framework?

0

Context: Home I'm looking to load the Template Checklist templates in the combo, but only those that are not in the structure checklist database table. In this case, only the Catu model would be loaded, since it is not registered in the structure checklist table. How to solve this problem?



ERmodel:

Queryingthedatafromthechecklist_model

Querydatafromthestructurechecklist(doesnothavethe'catu'model=8)




Controller Checklist StructureController.php contains the new method (): responsible for loading the model combo.

 //Este método apresenta o formulário para cadastrar um novo checklists de estruturas
       public function novo()
       {

          // $checklistEstrutura =  ChecklistEstrutura::all();

           $checklistModelo = ChecklistModelo::all();

          $checklistEstrutura = DB::table('checklist_estrutura')
           ->join('checklist_modelo', 'checklist_modelo.id', '=', 'checklist_estrutura.modelo_id')
           ->join('checklist_itens', 'checklist_itens.id', '=', 'checklist_estrutura.itens_id')
           ->select('checklist_estrutura.modelo_id', 'checklist_modelo.modelo', 'checklist_modelo.ativo')
           ->groupBy('checklist_estrutura.estrutura_id', 'checklist_estrutura.modelo_id')
           ->distinct()
           ->get(); 



           //testando a variável $checklistModelo
         $checklistModelo = ChecklistModelo::where('id','<>', $checklistEstrutura->modelo_id);



           $checklistItem =  ChecklistItem::all();
              return view('admin.checklistEstrutura.novo',['checklistsEstruturas' => $checklistEstrutura, 'checklistsModelos' => $checklistModelo,
              'checklistsItens' => $checklistItem]);
       }


I tried filtering this way

  

$ checklistModel = ChecklistModel :: where ('id', '< >',   $ checklistStructure-> template_id);

But the following error occurred:

  

Property [template_id] does not exist on this collection instance.

    
asked by anonymous 19.09.2018 / 22:12

2 answers

1

In addition to @Weslei replied, since you want to ignore a list of items, you can combine the pluck and whereNotIn , like this:

ChecklistModelo::whereNotIn('id', $checklistEstrutura->pluck('modelo_id'))->get();

Using pluck , it will return a array with all modelo_id found, then the whereNotIn function will make a filter bypassing records with id matching those in array .

    
19.09.2018 / 23:02
0

The model_id error is occurring because when you perform the query checklist Structure, you are using the get () method, this function always returns an array, so that you can access a property of your model, it must be an element specific, not an array, after the get () method, you can use find ().

    
19.09.2018 / 22:26