vendredi 9 février 2018

Foreach inner When query Laravel

Hi Guys this is my function, i am using laravel 5.3

public function search(Request $request){

    $word = $request->search;
    $status = explode(",", $request->status);
    $type = explode(",", $request->type);
    $beds = $request->beds;
    $baths = $request->baths;
    $amenities = explode(",", $request->amenities);
    $typeCount = 0;
    $amenitiesCount = 0;

    $query = DB::table('properties as pr')
        ->join('status as st', 'pr.status_id', '=', 'st.id')
        ->join('types as ty', 'pr.type_id', '=', 'ty.id')
        ->join('property_has_features as prfe', 'prfe.property_id', '=', 'pr.id')
        ->join('features as fe', 'prfe.feature_id', '=', 'fe.id')
        ->where('pr.title', 'like', '%'.$word.'%')
        ->when($status, function ($query) use ($status) {
            $statusCount = 0;
            foreach ($status as $v) {
                if ($statusCount > 0){
                    return $query->orwhere('st.title', $v);
                }

                return $query->where('st.title', $v); 
                ++$statusCount;
            }
        })->toSql();

    dd($query);
  }

}

And i need to search for multiple values but if your search multiple fields i have a query to myqsl manual but now i am converting to laravel but the consult only make one where to st.title although the $status is a array with two values or more never make the if inner when, this is the consult i return for the debug, any help thanks

"select * from `properties` as `pr` inner join `status` as `st` on 
`pr`.`status_id` = `st`.`id` inner join `types` as `ty` on 
`pr`.`type_id` = `ty`.`id` inner join `property_has_features` 
as `prfe` on `prfe`.`property_id` = `pr`.`id` inner join `features` 
as `fe` on `prfe`.`feature_id` = `fe`.`id` where `pr`.`title` 
like ? and `st`.`title` = ?"



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire