How to filter Combobox data using Laravel Framework?

Asked

Viewed 164 times

0

Context:

I want to carry the models of Template Checklist in the combo, but only those that are not in the database table checklist_structure. In this case, only the Catu model would be loaded, because it is not registered in the table checklist_structure. How to solve this problem?

inserir a descrição da imagem aqui

Model ER: inserir a descrição da imagem aqui

Query of checklist_template data

inserir a descrição da imagem aqui

Query of checklist_structure data (does not have model 'Catu' = 8)

inserir a descrição da imagem aqui


Checkliststructurecontroller.php controller 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 to do the filtering that way

$checklistModel = Checklisttemplate::Where('id','<>', $checklistEstrutura->modelo_id);

But the following error occurred:

Property [modelo_id] does not exist on this Collection instance.

  • if you want to continue giving the Get simply do the following: after you search the checklisttemplate check if it exists: if(isset($checklistModelo[0]->modelo_id)){ &#xA;$checklistItem = ChecklistItem::all();&#xA;return view('admin.checklistEstrutura.novo',['checklistsEstruturas' => $checklistEstrutura, 'checklistsModelos' => $checklistModelo,&#xA; 'checklistsItens' => $checklistItem]);&#xA; }else{&#xA; return back()->withErrors(['Error'=>'Não foi possível realizar a busca!' ]);&#xA;} remembering that in this case you will need to use a foreach on Blade

  • Thank you for the reply.

2 answers

2

In addition to what @Weslei replied, as you want to ignore a list of items, you can combine the function pluck and whereNotIn, thus:

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

Using the pluck, he will return a array with all the modelo_id found, then the function whereNotIn will make a filter ignoring the records with the id that match the ones on array.

  • thanks for your help. I tested the code you reported and it worked.

0

The modelo_id error is occurring because when you are performing the checklistEstrutura query, you are using the get() method, this function always returns an array, so for you to be able to access a property of your model, it must be a specific element and not an array, after get() method, you can use find().

  • Thank you for the reply.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.