Condition Checking In Laravel

Hello guys ! Today we are going to learn how we can easily apply where clauses in Laravel. We will also learn different techniques to check multiple condition. For example, how to check condition in query builder and many more things. This blog will help you to improve your coding style, reduce the lines of code and make your code more readable. Here I’ll show normal techniques and advanced techniques to help you with it. So, let’s begin.

1. Where X

This is a magic method or also called as a dynamic method of Laravel where we can check conditions using column names. I know you are slightly confused but yes you can directly check the condition using column name. Let’s take an example and see the results.

 Old technique

$user = User::where(‘name’, ‘alen’)->get();

New technique

$user = User::whereName(‘alen’)->get();

I know you guys are surprised but yes you can use this magic method to check the condition. Just write your column name in the title case with where method. 



2. When Method

This is not an actual ‘where’ condition but this method will check the conditions. This is an eloquent technique where you can check parameters, column names etc. Let’s take an example :

Old technique

$users = User::where(‘active’, ‘y’);
if( $request->filter == ‘date’ ) {
	$users->where(‘date’, $request->date);

if( $request->filter == ‘time’ ) {
	$users->where(time’, $request->time);
		$users = $users->get();

New Technique

$user = User::whereActive(‘y’)
		->when($request->filter == ‘date’, function($query) use ($request) {
		$query->where(‘date’, $request->date);
where($request->filter == ‘time’, function($query) use ($request){
	$query->where(‘time’, $request->time);

Here we can also check the when condition with column name also you can check else condition by passing the second closure. For example, if we need to apply conditions based on users subscription like if a user is subscribed then minimum 100 points are required otherwise 300 points are required, then

$users = User::whereActive(‘y’)
->when(subscribed’ == ‘y’, function ($query) {
	$query->where(‘points’, ‘>=’, 100);
}, function($query){
	$query->where(‘points’, ‘>=’, 300);

3. Where & OrWhere using closure

This is a very common technique and I hope you guys are currently using this technique in your current development. Still, let’s go with an example to get better clarity.

To understand this technique we take an example like, user must be subscribed and either user’s points are greater than 300 or user must be verified. So there on is the main condition and the rest of two vice versa.

$user = User::whereSubscribed(‘y’)
				->where(function($query) {
					$query->where(‘points’, ‘>=’, 300)
						->orWhere(‘verified’, ‘y’);

4. Pre-defined whew method for date/time 

There are  4 per-define methods to check date/time in Laravel. These methods are only with date, date/time and timestamp data-types.

Check complete date

$date = Carbon::today()->subDays(15)->format(‘Y-m-d’);
$user = User::whereDate(‘created_at’, $date)->get();

Check by day

$day = Carbon::today()->format(‘d’);
$user = User::whereDay(‘created_at’, $day)->get();

Check by month

$month = Carbon::today()->format(‘m’);
$user = User::whereMonth(‘created_at’, $month)->get();

Check by year

$year = Carbon::today()->format(‘y’);
$user = User::whereYear(‘created_at’, $year)->get();

That’s it for this blog. Hope you were able to learn some new techniques for conditioning.

Leave a Reply

Your email address will not be published. Required fields are marked *