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

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

问题描述

我正在使用最新的Laravel安装。

I am using latest Laravel install.

我的表结构是这样的: https://www.uploady.com/#!/download/OQSH4ualzUD/x2SIqdcaulqzSSsy

My table structure is this: https://www.uploady.com/#!/download/OQSH4ualzUD/x2SIqdcaulqzSSsy

我试过这些查询:

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()

理想情况下,它应该返回最后3条记录。但它返回空白数组。如何解决这个问题?请帮助。

Ideally, it should return last 3 records. But it returns blank array. How do I tackle this? Please help.

Code::all()

返回所有4条记录。

代码模型:

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

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('App\CodeType');
    }

}


推荐答案

使用其中!= 运算符结合 whereNull

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

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

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