1

I'm using PHP laravel framework
what I want is accessing query whereClause parameters as JSON and converting it to a string query for reading records from database
example:

converting this:

{"working_years":{">":2,"<":6},"car_name":"someName","car_company":"companyName"}  

to:

Select * from my_table where working_years > 2 AND working_years < 5 AND car_name=someName AND car_company = companyName

the important part for me is "where" conditions
here is my code:

    // $query=$request->where;
    //for now we access it from a string:
    $query = '{"working_years":{">":2,"<":6},"car_name":"someName","car_company":"companyName"}';

    if (isset($query)) {


        $i = 0;
        $whereFieldsAndVals = array([]);// ex:-> [10][name,abc]

        try {

            $operator = array([]);// ex:-> [10][=,>]
            foreach (json_decode($query) as $key => $value) {


                if (is_object($value)) {//has custom conditions

                    $j = 0;
                    foreach ($value as $k => $v) {

                        $operator[$i][$j] = $k;
                        $whereFieldsAndVals[$i][1] = $v;
                        $j++;
                    }

                } else {
                    $whereFieldsAndVals[$i][1] = $value;
                }
                $whereFieldsAndVals[$i][0] = $key;



                $i++;

            }
        } catch (\Exception $exception) {
            return $this->customError($exception->getMessage(), 30, 500);
        }

                }

                return $operator;
                //this will return:
                //[[">","<"]]

                //return $whereFieldsAndVals;
                //this will return:
                //[{"1":6,"0":"working_years"},{"1":"someName","0":"car_name"},{"1":"companyName","0":"car_company"}]

}
Winner1
  • 1,261
  • 2
  • 12
  • 17
  • What have you tried so far? – Rwd Aug 20 '18 at 16:05
  • @RossWilson Wilson i stored normal parameters and values + conditions in an array but I don't know how to store and convert them to string ,i will send the code shortly – Winner1 Aug 20 '18 at 16:07

1 Answers1

0

finally got the solution
according to Jarek Tkaczyk answer in this thread: How to create multiple where clause query using Laravel Eloquent?
we can run our where clause queries using eloquent in this template:

where([
    ['column_1', '=', 'value_1'],
    ['column_2', '<>', 'value_2'],
    [COLUMN, OPERATOR, VALUE],
    ...
])

here is my new code:

    if (isset($query)) {
                $fullQuery = [];
                /*
                 https://stackoverflow.com/questions/19325312/how-to-create-multiple-where-clause-query-using-laravel-eloquent
                 template:
                 where([
                 [COLUMN, OPERATOR, VALUE],
                 ['column_1', '=', 'value_1'],
                 ['column_2', '<>', 'value_2'],
                */

                try {
     //  $query = '{"working_years":{">":2,"<":5,"<>":3},"car_name":"bmw","car_company":"am84"}';
                    $i = 0;
                    foreach (json_decode($query,true) as $key => $value) {

                        if (is_array($value)) {//has custom where condition

                            $count=count($value);
                            $j = 0;
                            foreach ($value as $whereKey => $whereValue) {
                                $fullQuery[$i+$j][0]=$key;
                                $fullQuery[$i+$j][1]=$whereKey;
                                $fullQuery[$i+$j][2]=$whereValue;
                                $j++;
                            }
                            if ($count>0)
                            $i+=($count-1);
                        } else {
                            $fullQuery[$i][0]=$key;
                            $fullQuery[$i][1]='=';
                            $fullQuery[$i][2]=$value;
                          }


                        $i++;

                    }
                } catch (\Exception $exception) {
                    return $this->customError($exception->getMessage(), 30, 500);
                }
                return $table->where($fullQuery)->first();
}
Winner1
  • 1,261
  • 2
  • 12
  • 17