0

I am using below code to fetch records from database in Laravel

$products = new Product();
$products = $products->orderBy('id', 'DESC')->paginate(10);

But it's throwing exception

PDOException SQLSTATE[HY093]: Invalid parameter number

It's also showing below error:

Illuminate\Database\QueryException SQLSTATE[HY093]: Invalid parameter number (SQL: select count(*) as aggregate from 'tbl_products' where 'tbl_products'.'deleted_at' is null and 'category_id' = 10 and created_at between and ?)

I tried to find a solution which is already posted here but didn't work for me.

Before executing above code I've written code in a function and I think that code is affecting.

$timeframe = Input::get('daterange');
$products = $products->whereBetween('created_at', $timeframe);
  • First see `dd($products);` Is it gives you results? Also, try to change variable name after `$products = new Product();` – Hiren Gohel Sep 11 '17 at 06:10
  • I don't see any errors in the following statements. Have you checked whether some other line maybe throwing this error? – Deepansh Sachdeva Sep 11 '17 at 06:11
  • Where is the code that is generating the `created_at between` (`whereBetween`)? – Rwd Sep 11 '17 at 06:21
  • @RossWilson Nice question, I tried to look into the code and found that I've written a function in which other query executing related to `created_at`. There is something weird because the error is pointing to another code line which I've described in the question. So editing the question and adding that code which I wrote in a function. –  Sep 11 '17 at 06:32
  • I think you get `NULL` for `$timeframe` Had you got `daterange` from input?? If yes, please post it here what format it gives you! – Hiren Gohel Sep 11 '17 at 06:38
  • @HirenGohel Thanks for the suggestion. It may possible that $timeframe is having NULL value, let me debug my code again because only sometimes I'm getting this error not every time. –  Sep 11 '17 at 06:45
  • Ok, if you get result, plz post it here..what format you get! – Hiren Gohel Sep 11 '17 at 06:46
  • `$timeframe` needs to be array with 2 elements as start & end date with proper mysql date format. Verify what you are getting in that variable @Onkar – Deepansh Sachdeva Sep 11 '17 at 07:20

1 Answers1

2
$products = new Product();
$products = $products->orderBy('id', 'DESC')->paginate(10);

This does not fetch records - you are creating a new, empty product object, and then trying to query it.

To retrieve all Products and order and paginate them, try this:

$products = Product::orderBy('id', 'DESC')->paginate(10);
Don't Panic
  • 13,965
  • 5
  • 32
  • 51