1

I have used below query to get result with some codition in my controller

$events = Event::whereHas('topic', function ($q) {
               $q->where('delete_status', 0);
            })
            ->where('status', 1)
            ->where('delete_status', 0);

And in my view file i have used this variable thrice to check with Date like below

$events->where('type', '!=', 2)->whereDate('start_date', '>=', \Carbon\Carbon::now())->get()

And

$events->where('type', 2)->whereDate('start_date', '>=', \Carbon\Carbon::now())->get()

And

$events->whereDate('start_date', '<', \Carbon\Carbon::now())->get()

Because i want to get the result based on past date or present date. If i use get in controller query i got error of whereDate() does not exists So i have used get() method in view file.

I cannot get record from second and third query but I can only get record from 1st query in view file.

Any solution?

VinoCoder
  • 1,133
  • 5
  • 22
  • 45

1 Answers1

3

$events is an collection which doesn't have whereDate method, what you might be looking for is the filter() to filter your collection in your views. Hence you could do so

$filtered_events = 
    $events->where('type', 2)
           ->filter(function ($item) {
                 return Carbon::parse($item->start_date)->gte(Carbon::today());
             });

Reference

linktoahref
  • 7,812
  • 3
  • 29
  • 51