jamesaps's avatar

Eloquent for orWhere isNull

Can I use eloquent like this:

$query->where($field, $value)->orWhereIsNull($field)->orWhereNotIn($field, $array)->get();

?

Basically can I preface any advance where function with 'or' and will it still work?

0 likes
7 replies
RachidLaasri's avatar

No, you can just preface any where method with 'or', these are built in methods.

For situations like this I prefer using whereRaw.

RachidLaasri's avatar

I didn't get what you meant, can you clarify please ?

anyway, here's an example from Laravel documentation :

$users = User::whereRaw('age > ? and votes = 100', array(25))->get();

And this is more advanced where methods you can use

DB::table('name')->whereBetween('column', array(1, 100))->get();
DB::table('name')->whereIn('column', array(1, 2, 3))->get();
DB::table('name')->whereNotIn('column', array(1, 2, 3))->get();
DB::table('name')->whereNull('column')->get();
DB::table('name')->whereNotNull('column')->get();

Source : http://cheats.jesse-obrien.ca/

2 likes
jamesaps's avatar

@rachidlaasri

I'm basically asking if these methods exist in Eloquent:

DB::table('name')->orWhereBetween('column', array(1, 100))->get();
DB::table('name')->orWhereIn('column', array(1, 2, 3))->get();
DB::table('name')->orWhereNotIn('column', array(1, 2, 3))->get();
DB::table('name')->orWhereNull('column')->get();
DB::table('name')->orWhereNotNull('column')->get();
RachidLaasri's avatar
Level 41

Yes, they do exists and you can use them with Eloquent too.

4 likes

Please or to participate in this conversation.