Laravel! = Operator where does not work

This request returns null when an object is expected.

$vow = DB::table('media_featured')->where('is_video_of_the_week', 1)-> where('video_of_week_expired', '!=', 1)->first(); CREATE TABLE `media_featured` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `media_id` int(10) unsigned DEFAULT NULL, `is_video_of_the_week` tinyint(1) DEFAULT NULL, `is_featured` tinyint(1) DEFAULT NULL, `video_of_week_expired` tinyint(1) DEFAULT NULL, `featured_expired` tinyint(1) DEFAULT NULL, `created_at` timestamp NULL DEFAULT NULL, `updated_at` timestamp NULL DEFAULT NULL, `deleted_at` timestamp NULL DEFAULT NULL, PRIMARY KEY (`id`), KEY `media_featured_media_id_foreign` (`media_id`), CONSTRAINT `media_featured_media_id_foreign` FOREIGN KEY (`media_id`) REFERENCES `media` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci; 

The recording may have is_video_of_the_week = 1 and video_of_week_expired = NULL , but the above query returns null.

Any ideas?

+6
source share
3 answers

NULL values ​​are not equal or not equal to anything else.

So column != NULL always false, and column = NULL

To check if a column contains a NULL value, you need to use the IS NULL statement.

In the case of laravel db query generator you can use

 ->whereNull('video_of_week_expired') 

method.

PS: if video_of_week_expired is considered a column with the same flag, you should make it NOT NULL and use video_of_week_expired instead of NULL / 1

+7
source

If the value of video_of_week_expired is NULL or 1 , you can use

-> whereNull ()

even if the value is like the flag 0 or 1 , then you can try using

-> where ('video_of_week_expired', '<>', 1)

Here <> is a non-equal operator.

+8
source

Based on documentation and source code .

You should use whereNull :

 $vow = DB::table('media_featured') ->where('is_video_of_the_week', 1) ->whereNull('video_of_week_expired') ->first(); 
0
source

All Articles