Laravel Eloquent Tips and Tricks

Laravel Eloquent Tips and Tricks

Here is the list of some of the useful Laravel Eloquent Tips and Tricks which are usually ignored by developers and not used as much as these should be.

1.) Expressive where syntax

$products = Product::where('category' , 3);

you can do this instead

$product = Product::whereCategory(3);

just appendĀ column name in plural case after where

2.) Date filtering

$q->whereDate('created_at', date('Y-m-d'));
$q->whereDay('created_at', date('d'));
$q->whereMonth('created_at', date('m'));
$q->whereYear('created_at', date('Y'));

Continue reading