Quantcast
Channel: Active questions tagged javascript - Stack Overflow
Viewing all articles
Browse latest Browse all 140161

Dynamic build a query in Eloquent as user fills Checkboxes, Laravel Blade

$
0
0

So, basicaly, i'm doing a method that users fills a dynamic list of checkboxes, to use it as options so it filters a datatable and consequently export it as CSV.

In blade view i'm filling a JSON with 2 kinds of objects, "Attributes" and "Locations", dynamic sized as users fills checkboxes, so i don't know which the user fills and how many.

queryParameters.attributes.push(attribute);
...
queryParameters.locations.push(location);
...

If i print the JSON before sending it i got this structure

console.log("client queryParameters: " + JSON.stringify(queryParameters));
{
   "attributes":[
      {
         "table":"attributes_2",
         "id":4,
         "name":"Mobiliário",
         "isChecked":true
      },
      {
         "table":"attributes_2",
         "id":5,
         "name":"Informática",
         "isChecked":true
      }
   ],
   "locations":[
      {
         "table":"resale_costumer",
         "country":"Moçambique",
         "isChecked":true
      },
   ]
}

and sending the JSON data to controller with axios

axios.get('/customers/resale/filterToCSV', {
            params: {
                dataFromClient: queryParameters,
          }
})

in web.api the route is defined like this

Route::get('/customers/resale/filterToCSV', 'Resale_customerController@getFilteredQueryResults');

and then, in the server side I don't know how to dynamic build the query as looping the $data ... any ideas how to achieve this? it's an example here.


    public function getFilteredQueryResults(Request $request){

        $data = json_encode($request->dataFromClient);

        /* asdasdasd */
        $results = DB::select(DB::raw(

            "SELECT * FROM resale_customer 

            /* LOOP THROUGH $data */

            WHERE 'District' = '$data["Locations"]["Country"]'"   
            OR 
            WHERE 'Attribute' = '$data["Attributes"]["name"]'" 

            /* END LOOP */

            )  );


        return $this->generateCSV($results);

    }

I'm using Laravel 6, blade, php in server side... Thank you!


Viewing all articles
Browse latest Browse all 140161

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>