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();
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 .
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.
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. }
DB::unprepared - Option to return false on failure Upon a successful entry the method return true and all works as expected.
Use whereNull
$shipment_data = DB::table('shipment')
->whereNull('pdf_url')->get();
try this:
$records = DB::table('shipment')
->where('shipment_date','2017-12-11')
->whereNull('pdf_url')
->get();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With