Laravel eloquent provides multiple ways to build the query one the of the feature of laravel eloquent is creating dynamic query based on condition or complicated queries.In this article i will show you to build where condition in laravel with example. I will show you multiple example to create where condition and also the complicated queries.
Here is the simple syntax to use the laravel where query
Syntax to use
where('COLUMN_NAME', 'OPERATOR', 'VALUE')
//or
where('COLUMN_NAME', 'VALUE'); // in this case operator always will be `=`
//or
where('RAW_QUERY_USING_DB_FACADE')
//OR
whereColumnName('VALUE');
Sql of above query will be
select * from `table` where `COLUMN_NAME` = 'VALUE'
As you can see i have written multiple way to use same method with different parameters. So lets begin with simple examples
Example 1 – Simple where example
In this example i will show you simple where condition with two parameters
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Article = Article::select("*")
->where("title", 'test')
->get();
\\or
DB::table('articles')->where("title","test")->get();
}
}
Output wil be
select * from `articles` where `title` = 'test'
Example 2 – Passing operator to where
In this example we will use operator like greater then, less then, equals to, like etc.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Article = Article::select("*")
->where("title","like" ,'test')
->get();
\\or
DB::table('articles')->where("title","like","%test%")->get();
\\
\\or
DB::table('articles')->where("status",>=,1)->get();
}
}
Output
select * from `articles` where `title` like 'test'
#or
select * from `articles` where `title` like '%test%'
#or
select * from `articles` where `status` >= 1
Example 3 – Multiple where condition
In this example we will use operator like greater then, less then, equals to, like etc.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Article = Article::select("*")
->where(['title'=>"test","status"=>'1'])
->get();
//or
$Article = Article::select("*")
->where("title","like" ,'test')
->where("status", 1)
->get();
\\or
DB::table('articles')->where(['title'=>"test","status"=>'1'])->get();
}
}
Output
select * from `articles` where `title` = 'test' and `status` = 1
Example 4 – Complicated Raw query in where
In this example we will use DB::raw
to use complicated query in our laravel application. Sometimes we wanted to use aggregate function in laravel where so
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use DB;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Article = Article::select("*")
->where(DB::raw("concat(first_name,'', last_name )"),"test test2")
->get();
}
}
Output
select * from `articles` where concat(first_name,'', last_name ) = 'test test2'
Example 5 – Add brackets to where condition
In this example we will use DB::raw
to use complicated query in our laravel application. Sometimes we wanted to use aggregate function in laravel where so
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\User;
use DB;
class ArticleController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$Article = Article::select("*")
->where("title","test")
->where(function($q){
return $q->where("status",1)->where('is_active',1);
})
->get();
}
}
Output
select * from `articles` where `title` = 'test test2' and (`status` = 1 and `is_active` = 1 )