Laravel Eloquent Where Query Examples

By Hardik Savani | July 17, 2020 | 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 and laravel 8.

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 : Laravel


Hardik Savani
My name is Hardik Savani. I'm a full-stack developer, entrepreneur and owner of Aatman Infotech. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Javascript, JQuery, Laravel, Codeigniter, VueJS, AngularJS and Bootstrap from the early stage.
Follow Me: Github Twitter
***Do you want me hire for your Project Work? Then Contact US.

We are Recommending you: