Create query with whereNotIn in laravel

1

I have two tables junta , voto and I want to obtain the meetings that do not have registered votes.

I've tried it in the following ways and none shows the expected result.

//TODO::forma 1
    $juntasVotos = DB::table('voto')->where('vot_estado', '=', 1)->get();
    foreach ($juntasVotos as $jv) {
        $data[] = $jv->junta_idjunta;
    }
    $data2 = array_unique($data);
    $junta1 = DB::table('junta')->whereNotIn('idjunta', $data2)->get();

    //TODO::forma 2
    $junta2 = DB::table('junta')->where('jun_estado', '=', 1)
        ->whereNotIn('idjunta', function ($query) {
            $query->select('junta_idjunta')
                ->from('voto');
        })->get();

    //TODO::forma 3
    $junta3 = DB::table('junta')
        ->select('idjunta')
        ->whereNotIn('idjunta', DB::table('voto')->pluck('junta_idjunta'))
        ->get();

    dd($junta3);
    
asked by jeancarlos733 05.11.2018 в 18:45
source

1 answer

1

The solution to my problem was the following, first the variable data[] has records with the same junta_idjunta , so first you should clean the duplicate records with the following function array_unique and reindex them with the function array_values .

    $juntasVotos = DB::table('voto')->where('vot_estado', '=', 1)->get();
    $listas = lista::where('lis_estado', '=', 1)->get();

        foreach ($juntasVotos as $jv) {
            $data[] = $jv->junta_idjunta;
        }

        $data2 = array_values(array_unique($data));
        $junta1 = DB::table('junta')->whereNotIn('idjunta', $data2)->get();
    
answered by 06.11.2018 в 05:51