2015-01-31 20 views
33

Sto usando l'ultima installazione di Laravel.Eloquent - dove non uguale a

La mia struttura della tabella è questo: https://www.uploady.com/#!/download/OQSH4ualzUD/x2SIqdcaulqzSSsy

ho provato queste query:

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

Idealmente, dovrebbe restituire ultimi 3 record. Ma restituisce matrice vuota. Come posso affrontare questo? Per favore aiuto.

Code::all() 

restituisce tutti e 4 i record.

modello Codice:

<?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

Si prega di spiegare il downvote. Correggerò la domanda – aBhijit

risposta

82

Usa where con un != operatore in combinazione con whereNull

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

Hmm questo dovrebbe funzionare. Suppongo che l'errore sia da qualche altra parte. Puoi aggiornare la tua domanda con un po 'di più del tuo codice per favore? – lukasgeiter

+1

Sta ignorando i record NULL. Se cambio uno dei NULL su un ID non NULL diverso da 2, viene restituito quel record. Con "it", intendo MySQL. – aBhijit

8

Per where field not empty questo ha funzionato per me:

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