Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Query Builder where is NULL no result [duplicate]

I have code as below to get shipment data where pdf_url is not NULL;

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', ['pdf_url', '<>', 'NULL']])->get();

This has no problem, I get the data I need, but when I'm trying to use the same code to get the data with pdf_url is NULL, it has no result.

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', ['pdf_url', '=', 'NULL']])->get();

What do I missing? I am very sure the DB record is there. I also tried other formats but still no result;

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', ['pdf_url', 'NULL']])->get();

And

$shipment_data = DB::table('shipment')->where([
 'shipment_date' => '2017-12-11', 'pdf_url' => 'NULL'])->get();

EDIT: I can use whereRaw, but I'll prefer to use where instead. Code below has no issue;

$shipment_data = DB::table('shipment')
 ->whereRaw('shipment_date = "2017-12-11" AND pdf_url is NULL')->get();
like image 365
sulaiman sudirman Avatar asked Dec 12 '17 02:12

sulaiman sudirman


People also ask

Is null in where in laravel?

Check if not null: whereNotNullSELECT * FROM users WHERE last_name IS NOT NULL; The equivalent to the IS NOT NULL condition in Laravel Eloquent is the whereNotNull method, which allows you to verify if a specific column's value is not NULL .

How check result is empty in laravel?

When using Laravel Eloquent, we may have to check a return value from database query is empty or not. But the truth is method is_null() or method empty() can not work.

How does laravel handle query exception?

The simplest way to catch any sql syntax or query errors is to catch an Illuminate\Database\QueryException after providing closure to your query: try { $results = \DB::connection("example") ->select(\DB::raw("SELECT * FROM unknown_table")) ->first(); // Closures include ->first(), ->get(), ->pluck(), etc. }

What is DB :: unprepared in laravel?

DB::unprepared - Option to return false on failure Upon a successful entry the method return true and all works as expected.


2 Answers

Use whereNull

$shipment_data = DB::table('shipment')
            ->whereNull('pdf_url')->get();
like image 52
sumit Avatar answered Sep 27 '22 23:09

sumit


try this:

$records = DB::table('shipment')
  ->where('shipment_date','2017-12-11')
  ->whereNull('pdf_url')
  ->get();
like image 27
Ajay Avatar answered Sep 27 '22 21:09

Ajay