I want to make the correct filter for the price, but it gives an error.

In the table there are 2 fields with prices price_weekend_max and price_weekend_max_comm . When outputting these prices, I add them, ie $c->price_weekend_max + $c->price_weekend_max_comm and it turns out one single price at which I try to make a filter.

The filter works according to the following principle: the usual slider from one price to another min / max prices are transferred to the method and there I want to filter the data by request.

 $filtered_items = Cottage::orderBy('priority', 'desc'); $filtered_items = $filtered_items ->where('price_weekend_max + price_weekend_max_comm','>=', $data['price_from']) ->where('price_weekend_max + price_weekend_max_comm','<=', $data['price_to']); 

When the request is an error:

  SQLSTATE[42S22]: Column not found: 1054 Unknown column '47000' in 'where clause' (SQL: select count(*) as aggregate from `cottages_new` where `persons_min` >= 1 and `47000` >= 0 and `47000` <= 0 and `publish` = 1) 
  • where('тут должно быть название колонки', ...)->get() , and you have not a name, but probably a value .. - entithat
  • Yes, this is the value, so I can’t figure out how to do it right - Sergey Babiy

2 answers 2

Look at the error. MySQL cannot find the column 47000 in the database, so the code that you showed is clearly not enough to solve the problem. See what $data stores in itself, make a sql query debug.

Convenient debugging package: https://github.com/barryvdh/laravel-debugbar

  • $ data stores the amount that is transferred from the filter form, I need to add the sums from these two columns and find in the database records that are in the range of amounts received from $ data ['price_from'] and $ data ['price_to'] - Sergey Babiy

Should work:

 $filtered_items = Cottage::orderBy('priority', 'desc'); $filtered_items = $filtered_items ->where((int)'price_weekend_max + price_weekend_max_comm','>=', $data['price_from']) ->where((int)'price_weekend_max + price_weekend_max_comm','<=', $data['price_to']); 
  • SQLSTATE error [42S22] failed: Column not found: 1054 Unknown column '0' in 'where clause' (SQL: select count (*) as aggregate from cottages_new where persons_min > = 1 and 0 > = 0 and 0 <= 26000 and publish = 1) - Sergey Babi