on this article, We’ll discover Laravel’s whereNull methodology with examples. The whereNull
methodology helps to filter data primarily based on null values. it creates a easy question As an alternative of writing complicated SQL queries.
Laravel offers a clear and readable syntax that enables builders to null-related queries into the desk. You may obtain this utilizing the whereNull eloquent methodology.
What’s Wherenull
Builders can filter data in Laravel’s Eloquent ORM (Object-Relational Mapping) database desk based on whether or not a given column is null or not by utilizing the whereNull methodology.
Additionally checkout different Laravel 10 tutorials,
Laravel whereNull With Examples
This methodology permits builders to question databases primarily based on the absence of a worth in a particular column.
The Syntax:
$nullStatusRecords = DB::desk('your_table') ->whereNull('col_name') ->get();
Let’s take a easy instance to filter data utilizing whereNull.
$nullRecords = Mannequin::whereNull('column_name')->get();
within the above snippet, It is advisable to change ‘column_name’ with the precise column title. This fetches all data the place the goal column worth is null.
Get all Workers which have E mail ID
Let’s retrieve all workers who haven’t supplied their e mail addresses:
$employeeWithoutEmail = Worker::whereNull('e mail')->get();
Utilizing orWhereNull
You may also mix a number of circumstances in a single question. The orWhereNull methodology can be utilized to incorporate data the place both of the required circumstances is met. Let’s take a easy instance:
$resp = Mannequin::the place('standing', '=', 'energetic') ->orWhereNull('standing') ->get();
The question will return the outcome which have a null standing in addition to with a standing of “energetic”.
Laravel whereNull With Fashions Relationships
You may also use whereNull with mannequin relationships. Laravel’s Eloquent permits builders to outline relationships between fashions. Like, I’ve an Worker mannequin with a one-to-many relationship with a Dept mannequin.
Let’s fetch these workers who haven’t related to any division, You should use whereDoesntHave
together with whereNull
:
$empsWithoutDept = Worker::whereDoesntHave('dept', operate ($question) { $query->whereNull('emp_id'); })->get();
The above question will filter workers who don’t have any related any division, and the nested whereNull question refines the outcome to incorporate solely these with out a specified emp ID within the dept desk.
Nullable Columns
You may also filter workers primarily based on column nullable
worth. The whereNull
methodology permits developer to filter out the info with nullable columns in database tables.
$res = Dept::whereNull('emp_id')->get();
Tender Delete Data Utilizing whereNull()
The whereNull
methodology can be helpful to get data which are “mushy deleted.” into the desk.
$activeEmp = Worker::whereNull('deleted_at')->get();
Conclusion
We’ve mentioned other ways to test the nullable values of a column. The whereNull methodology helps to seek out nullable columns, mushy deletes, and Fashions relations, whereNull permits builders to create efficient database queries.