Eloquent query inside foreach

Is it possible?
6 Replies
hosmar27
hosmar27OP6mo ago
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);
foreach($formularioIds as $formularioId){
// dd($formularioId->id);
$formulario = Formulario::query('ativo', true)->where('id', '=' , $formularioId)->where('pergunta', true)->get('texto')->toArray();
}
dd($formulario);
Brian Kidd
Brian Kidd6mo ago
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
hosmar27
hosmar27OP6mo ago
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: []
Dennis Koch
Dennis Koch6mo ago
You should use a whereIn instead
Brian Kidd
Brian Kidd6mo ago
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?
hosmar27
hosmar27OP6mo ago
Thank you, it worked $formulario = Formulario::query('ativo', true)->whereIn('id', $formularioId); thanks for the tip, I have already done it on the tabs in my question resource
Want results from more Discord servers?
Add your server