Eloquent - 不等于 [英] Eloquent - where not equal to

查看:41
本文介绍了Eloquent - 不等于的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前使用的是最新的 Laravel 版本.

I'm currently using the latest Laravel version.

我尝试了以下查询:

Code::where('to_be_used_by_user_id', '<>' , 2)->get()
Code::whereNotIn('to_be_used_by_user_id', [2])->get()
Code::where('to_be_used_by_user_id', 'NOT IN', 2)->get()

理想情况下,它应该返回除user_id = 2 之外的所有记录,但它返回空数组.我该如何解决这个问题?

Ideally, it should return all records except user_id = 2, but it returns blank array. How do I tackle this?

Code::all()

这将返回所有 4 条记录.

This returns all 4 records.

代码模型:

<?php namespace App;

use IlluminateDatabaseEloquentModel;

class Code extends Model
{

    protected $fillable = ['value', 'registration_id', 'generated_for_user_id', 'to_be_used_by_user_id', 'code_type_id', 'is_used'];

    public function code_type()
    {
        return $this->belongsTo('AppCodeType');
    }

}

推荐答案

where!= 运算符与 whereNull<结合使用/p>

Use where with a != operator in combination with whereNull

Code::where('to_be_used_by_user_id', '!=' , 2)->orWhereNull('to_be_used_by_user_id')->get()

这篇关于Eloquent - 不等于的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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