In the initial stage of laravel learning you will find many challenges to build the query like where not null and where null so in this article I will show you to create where not null and where null query using laravel eloquent.
To create where not null
in laravel eloquent we can use whereNotNull
method which accepts 1 parameter column name.
Here is the simple syntax to use the laravel whereNotNull query
Syntax to use:
whereNotNull('COLUMN_NAME');
SQL of above query will be
select * from `table` where `COLUMN_NAME` IS NOT NULL
So lets begin with simple examples
Example 1 – whereNotNull example
In this example i will show you simple whereNotNull
<?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("*")
->whereNotNull("category_id")
->get();
\\or
DB::table('articles')->whereNotNull("category_id")->get();
}
}
Output will be :
select * from `articles` where `category_id` IS NOT NULL
Laravel where null query in eloquent
To create where null
in laravel eloquent we can use wherNull
method which accepts 1 parameter column name.
Here is the simple syntax to use the laravel whereNull query
Syntax to use:
whereNull('COLUMN_NAME');
SQL of above query will be
select * from `table` where `COLUMN_NAME` IS NULL
So lets begin with simple examples
Example 1 – whereNull example
In this example i will show you simple whereNull
<?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("*")
->whereNull("category_id")
->get();
\\or
DB::table('articles')->whereNull("category_id")->get();
}
}
Output will be :
select * from `articles` where `category_id` IS NULL
Output
select * from `articles` where `category_id` in (select article_type_id from article_categories where category_id in (223,15) )