Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel ->when() with greater than or lesser than

I'd like to run different ->where() condition based to id.

Example what I'm trying to do:

I have joined activity table to interests table and based to ID I'd like to run different where condition.

 ->when('activity.id' <= '214469112', function ($q) {
    return $q->where('interests.type', 'regular');
       }, function ($q) {
       return $q->where('activity.main', 1);
       })->get();

However ->when() does not seem to respect <= => greater than or less than and returns always false so the return $q->where('activity.main', 1); is always used.

Any ideas how I could achieve different ->where() condition based to greater or less than with activity.id?

like image 797
EmJeiEn Avatar asked Oct 28 '25 16:10

EmJeiEn


1 Answers

Sigh, sometimes one just tries to overcomplicate the problem.

Did this by using:

Where('id', '>', int)->where('condition1')->orWhere('id', '<=', int)->where('condition2')->get();

like image 140
EmJeiEn Avatar answered Oct 31 '25 06:10

EmJeiEn