2015-01-31 93 views
33

我正在使用最新的Laravel安装。雄辩 - 不等于

我的表结构是这样的: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条记录。但它返回空白数组。我如何解决这个问题?请帮忙。

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'); 
    } 

} 
+4

请解释downvote。我会纠正这个问题。 – aBhijit 2015-01-31 21:54:17

回答

82

使用where与组合!=运营商whereNull

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

嗯,这实际上应该工作。我想这个错误是在别的地方。你可以用更多的代码更新你的问题吗? – lukasgeiter 2015-01-31 22:00:08

+1

它忽略NULL记录。如果我将其中一个NULL更改为2以外的某个非NULL id,则返回该记录。通过'它',我的意思是MySQL。 – aBhijit 2015-01-31 22:14:46

8

对于where field not empty这个工作对我来说:

->where('table_name.field_name', '<>', '')