skip to Main Content

What I want to do is something like this :


$listOfYears = Invoice::pluck('year') ; // [ 2001, 2002, 2003 ]
$products = Product::whereYearIn('purchase_date',$listOfYears)->get();

the field purchase_date is type date so you can’t just use whereIn, you need to apply whereYear 1st, then whereIn.

Obviously this won’t work because whereYearIn doesn’t exist …

I did this in the most slow & ugly way possible :


$listOfYears = Invoice::pluck('year') ; // [ 2001, 2002, 2003 ]
$products = Product::all()->filter(function($prod) {
     return in_array( date('Y',strtotime($prod->purchase_date)) , $listOfYears );
});

So I am asking if there is a way to achieve this ?
Also if the years could be preloaded in the same query with the method with it would be very optimized too.

Note:
This could be done easily in MySQL directly

SELECT * FROM products 
WHERE YEAR('purchase_date') in ( SELECT year FROM invoices ) ;

2

Answers


  1. You can use whereRaw() function in laravel eloquent. In raw query use extarct() part of MySQL it will return the year from date.

    $products = Product::whereRaw('extract(year from purchase_date) = ?', $listOfYears)->get();
    
    Login or Signup to reply.
  2. Try this one

    $products = Product::whereRaw("YEAR('purchase_date') in ?", $listOfYears)->get();
    

    Update

    $products = Product::whereRaw("YEAR(purchase_date) in ?", $listOfYears)->get();
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search