Laravel!=运算符在不起作用的地方 [英] Laravel != operator in where not working

查看:117
本文介绍了Laravel!=运算符在不起作用的地方的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当需要对象时,此查询返回null.

This query is returning 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;

一条记录可能具有is_video_of_the_week = 1video_of_week_expired = NULL,但以上查询返回空值.

A record might have is_video_of_the_week = 1 and video_of_week_expired = NULL but the above query returns null.

有什么想法吗?

推荐答案

NULL值不等于或不等于其他任何值.

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

所以column != NULLcolumn = NULL一样总是虚假的

要检查列是否包含NULL值,您需要使用IS NULL运算符.

To check if a column contains NULL value you need to use IS NULL operator.

在使用laravel db查询生成器的情况下,您可以使用

In case of laravel db query generator you could use

->whereNull('video_of_week_expired')

方法.

PS:如果假定video_of_week_expired是类似于标志的列,则最好将其设置为NOT NULL并使用0/1值,而不是NULL/1

PS: if video_of_week_expired is assumed to be a flag-alike column, you better make it NOT NULL and use 0/1 values, instead of NULL/1

这篇关于Laravel!=运算符在不起作用的地方的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆