How to use where clause with date_format() in Laravel?

Share Me
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Today now in this post, i will show you How to use where clause with date_format() in Laravel? Here i will share with you about how we can use date_format() aslo with where condition in Laravel query builder. When we are working on our project,some times i need to search with get only selected month and year records from based on created_at(timestamp) column. Here I am thinking to share with you how can we use date_format() of mysql. At last here i will use DB::raw() of Laravel and it’s work fine.

If you have also need to search on this way then you can also do it by this way:

Example:

$data = DB::table("items")
        ->select("id","title","created_at")
        ->where(DB::raw("(DATE_FORMAT(created_at,'%Y-%m'))"),"2016-07")
        ->get();
print_r($data);

Read Also : How to Use Group by year month using Laravel Query Builder ?

Thanks for read. I hope it help you. For more you can follow us on facebook.

About Shahriar Sagor

My name is Shahriar sagor. I'm a developer. I live in Bangladesh and I love to write tutorials and tips that will help to other Developer's. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.

View all posts by Shahriar Sagor →