#Eloquent query inside foreach

11 messages · Page 1 of 1 (latest)

main jetty
#

Is it possible?

mellow trenchBOT
#

To help others find answers, you can mark your question as solved via Right click solution message -> Apps -> ✅ Mark Solution

main jetty
#

my current code: foreach($formularioIds as $formularioId){ // dd($formularioId->id); $formulario = Formulario::query('ativo', true)->where('id', '=' , $formularioId)->where('pergunta', true)->get('texto')->toArray(); } dd($formulario);

cobalt fable
#

Why would you want to do that? What are you trying to accomplish? This isn’t a Filament question but I’ll try to help you if I can better understand what you are trying to do

main jetty
#

Thanks for your help sir, so basically what I'm doing is a quiz, and this is a part before I send all data to a form repeater and display all questions. So what is happening in the foreach is that it takes the formulario_id (translated is quiz_id) from the pivot table (called cargo_formulario) and I'm trying to search for all the 'pergunta' (question) and trying to display its 'texto' (text)

#

that ->toArray() is a mistake😅

#

the foreach is taking the right 'id' in that first dd(), but the secound one returns nothing, #items: []

fair pollen
cobalt fable
#

On your Cargo model, have you considered creating a many to many relationship named ‘questions’ that is the same as the relationship to Formulario but includes the where clause to only get the questions?

main jetty
#

$formulario = Formulario::query('ativo', true)->whereIn('id', $formularioId);