Laravel Where Clause with Mysql Function Example

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

Today now in this post i will show you laravel where clause with mysql function also with example.When we are working on laravel projects and we need to use mysql function in the where clause and then we can easily use that by using DB::raw() and whereRaw(). Now in this example i will show you how to i use mysql function in where clause. So now In this example i want to compare also with year of created_at field but not whole date, so that’s why i will use mysql function Year() function, this function will return us only year from timestamp and then compare with given value. Now I will add two example of how to use sql function in where cause.So let’s start and see the both example.

Example 1:
$data = DB::table("items")->select("items.*")
	     	->where(DB::raw("Year(items.created_at)"),'2016')
	     	->orderBy('items.created_at')
	     	->get();
Example 2:
$data = DB::table("items")->select("items.*")
       ->whereRaw(DB::raw("Year(items.created_at) = '2016'"))
       ->orderBy('items.created_at')
       ->get();

Read Also :Laravel Join with Subquery in Query Builder Example

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

About code chef

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 code chef →