Friday, March 31, 2017

Complex querie Laravel

Hi I have a complex queries

SELECT distinct libelle_adresse from adresse 
where id_adresse in (select id_adresse from zone_adresse 
where fk_zone in (select id_zone from zone 
where id_arrondissement in (select id_arrondissement from arrondissement
where libelle_arrondissement="'.$_GET['arrondis'].'")))

I've used $arrondis = $request->input('arrondis');to get the "arrondis" parameter from URL and pass it to my Controller.

This my attempt

    $term = $request->term;

    $arrondis = $request->input('arrondis');

    $query1 = DB::table('arrondissement')
    ->select('id_arrondissement')
    ->where('libelle_arrondissement','=',$arrondis)
    ->get();

    $query2 = DB::table('zone');
    ->select('id_zone')
    ->where('id_arrondissement','=',$query1)
    ->get();

    $query3 = DB::table('zone_adresse');
    ->select('id_adresse')
    ->where('fk_zone','=',$query2)
    ->get();

    $queries = DB::table('adresse');
    ->where('id_adresse','=',$query3)
    ->where('libelle_adresse','like','%'.$term.'%')
    ->take(3)->get();
    foreach ($queries as $query)
    {
        $results[] = ['id' => $query->id_adresse, 'value' => $query->libelle_adresse]; 
    }
    return response()->json($results);

I know that I get the error when I pass the "query(1/2/3) variable" in "where" but I couldn't find any solution

Any help would be appreciated. Thanks



via Marouane Sihad

Advertisement