Site Admin
Site Admin Founder of MeaningArticles
577 Views

Laravel Eloquent Where Query Examples

Hello Dev.

Today, i would like to show you laravel eloquent where condition. you will learn laravel eloquent where query. This article 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 application.

Syntax:

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


SQL Query:

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


Example 1:

<?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", "=", "[email protected]"]
                        ])
                        ->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'm hoping it assist you to, thanks for visit my article if you like my article then proportion together with your friend and social platform.