0

I want to filter some data using price but when we echo request value it give correct but when I put this in query it show "?"

my ajax call

<script>
        $(document).ready(function() {
            $("#price_filter").on("change", function() {
                var price = this .value;
                var _token = $('input[name="_token"]').val();
                $.ajax({
                    url:"{{ route('indexController.pricefilter') }}",
                    type:"POST",
                    dataType:"json",
                    data:{price,_token},
                    success: function(response){
                        console.log(response);
                        // $("#filterdata").html(response);
                    }
                });
            })
        })  
</script>

my route

Route::post('indexController/pricefilter','indexController@pricefilter')->name('indexController.pricefilter');

my function

public function pricefilter(Request $request){
    echo $price = $request->get('price');

    return $products = DB::table('products')
        ->where('price','=', $price)
        ->tosql();
}

my query result

400
select * from `products` where `price` = ?

it show actually like

select * from `products` where `price` = 400

Here is result

array:1 [
  0 => array:3 [
    "query" => "select * from `products` where `price` <= ?"
    "bindings" => array:1 [
      0 => "1001"
    ]
    "time" => 3.87
  ]
]
3
  • Check the output dd($request->price);, still null? Commented Sep 24, 2020 at 8:47
  • it show the value but query not filter data using price it return all data Commented Sep 24, 2020 at 10:34
  • but when we put this ->where('price','<', $prod_price) to ->where('price','<', 500) its work fine Commented Sep 24, 2020 at 10:43

2 Answers 2

0

you are seeing the question mark because laravel uses parameters binding

select * from products where price = ?

when the query excuted on db it will be select * from products where price = 400

anyway: if you want to your sql query with its parameters you can use this function:

public function getEloquentSqlWithBindings($query)
{
    return vsprintf(str_replace('?', '%s', $query->toSql()), collect($query->getBindings())->map(function ($binding) {
        return is_numeric($binding) ? $binding : "'{$binding}'";
    })->toArray());
}

now:

public function pricefilter(Request $request){
    $query= $products = DB::table('products')
            ->where('price','=', $price);
 echo($this->getEloquentSqlWithBindings($query));
return $query;
    }
Sign up to request clarification or add additional context in comments.

Comments

0

This is normal for Laravel.

To see the real database query you can use

DB::enableQueryLog();
// and then you can get query log
dd(DB::getQueryLog());

So your code would look like this

public function pricefilter(Request $request){
    echo $price = $request->get('price');

    DB::enableQueryLog();
    $products = DB::table('products')
        ->where('price','=', $price)
        ->get();
    dd(DB::getQueryLog());

    return $products;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.