Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

336
Visualizações
Select values in array, separate and query the database laravel

I have ids that are coming by array, I need to get them all and make selections in the bank using each one with different returns according to the selection. I am able to bring the numbers but it is not working to separate and use the select automatically according to the number of records in the array.

Below is the way I get the array:

Result Array $schools

Array
(
    [0] => stdClass Object
        (
            [id] => 1
            [school_type_id] => 2
            [external_id] => 
            [name] => School1
            [active] => 1
            [created_at] => 2020-02-21 23:42:11
            [updated_at] => 2020-02-21 23:42:11
            [deleted_at] => 
            [active_text] => Ativo

        )

    [1] => stdClass Object
        (
            [id] => 81
            [school_type_id] => 2
            [external_id] => 
            [name] => School2
            [active] => 1
            [created_at] => 2015-05-27 18:08:52
            [updated_at] => 2015-05-27 18:08:52
            [deleted_at] => 
            [active_text] => Ativo
           
        )

)

In the controller I'm using to bring only the values I want but I need to make a select with them and separate according to each one.

Controller.php

$schools = collect($api->schools);
    $info = $schools->pluck('id');

    for($i = 0;$i<count($info);$i++){

        $select = DB::table('sys_record')
        ->where('sys_record.id_step','<=',4)
        ->where('sys_users.schoolId','=',$info[$i])
        ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
        ->get();
        $information[] = count($select);  
        print_r($information);  
/*return Array
(
    [0] => 3
)
Array
(
    [0] => 3
    [1] => 1
)

        }*/

array 31 is being returned, I need 4

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

If you only need to get id values out in a separate array/collection, you can try the pluck method instead of the for loop:


// Get schools as a Collection
$schools = collect($api->schools);

// Pluck out all the 'id' key values
$info = $schools->pluck('id');
over 4 years ago · Santiago Trujillo Relatório

0

From what I understand you want to retrieve the sum of the results.

For that your code should look like this:

for($i = 0;$i<count($info);$i++){

    $select = DB::table('sys_record')
    ->where('sys_record.id_step','<=',4)
    ->where('sys_users.schoolId','=',$info[$i])
    ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
    ->get();

    $information[] = count($select);
}

$total = array_sum($information);

print_r( $total );

Or a bit nicer:

$schools = collect($api->schools);
$info = $schools->pluck('id');
$information = 0;

foreach( $info as $k => $i){
    $results_count = DB::table('sys_record')
      ->where('sys_record.id_step','<=',4)
      ->where('sys_users.schoolId','=',$info[$i])
      ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
      ->count();

    $information += $results_count;
}

print_r( $information );

EDIT Or even nicer ( not sure if this works, but you get the idea ). This will only run one query for all schools instead of running one separate query for each school:

$schools = collect($api->schools);
$info = $schools->pluck('id');

$results_count = DB::table('sys_record')
      ->where('sys_record.id_step','<=',4)
      ->whereIn('sys_users.schoolId', $info)
      ->join('sys_users', 'sys_users.id_jovens', '=', 'sys_record.id_user')
      ->count();

print_r( $results_count );
over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda