Laravel Eloquent Where Query Examples

By Hardik Savani November 5, 2023 Category : Laravel

Today, i would like to show you laravel eloquent where condition. you will learn laravel eloquent where query. This tutorial will give you simple example of where condition in laravel query builder. This tutorial will give you simple example of where clause in query builder of laravel 6, laravel 7, laravel 8, laravel 9 and laravel 10.

I will give you simple examples of how to use sql where query in laravel. just see bellow simple examples that will help you how to write database where condition in laravel 7 application.

Syntax:

where('COLUMN_NAME', 'OPERATOR', 'VALUE')

SQL Query:

select * from `users` where `status` = 1

Example:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", "=", 1)

->get();

dd($users);

}

}

Example 2: without Pass Operator

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("status", 1)

->get();

dd($users);

}

}

Example 3: Multiple Where Condition

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where([

["status", "=", 1],

["email", "=", "yemmerich@example.net"]

])

->get();

dd($users);

}

}

Example 4: Some more

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\User;

class UserController extends Controller

{

/**

* Display a listing of the resource.

*

* @return \Illuminate\Http\Response

*/

public function index()

{

$users = User::select("*")

->where("followers", ">", 100)

->get();

$users2 = User::select("*")

->where("followers", "<=", 1000)

->get();

$users3 = User::select("*")

->where("name", "like", "har%")

->get();

}

}

I hope it can help you...

Tags :