Laravel Where Clause with Mysql Function Example
In this post we will give you information about Laravel Where Clause with Mysql Function Example. Hear we will give you detail about Laravel Where Clause with Mysql Function ExampleAnd how to use it also give you demo for it if it is necessary.
When you are working on laravel projects and you need to use mysql function in where clause then you can easily use that using DB::raw() and whereRaw(). In this example you can show how to i use mysql function in where clause. In this example i want to compare with year of created_at field but not whole date, that’s whay i use mysql function Year(), this function will return only year from timestamp and compare with given value. I add two example of how to use sql function in where cause. let’s See 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();
Hope this code and post will helped you for implement Laravel Where Clause with Mysql Function Example. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs