protected function get_watchlist($user, $filter) { //Get all auctions from watchlist $query = DB::table('auctions')->select(DB::raw("auctions.*, watchlist.id as watchlist_id"))->join('watchlist', 'auctions.id', '=', 'watchlist.auction_id')->where('watchlist.user_id', $user); if ($filter != 'all') { $query->where('auctions.state', $filter); } $watchlist_items = $query->orderBy('expires', 'asc')->get(); // $watchlist_items = []; $watchlist_items = Auction::timeRemainingBatch($watchlist_items); //Count of records per state $all = count($watchlist_items); $active = 0; $ended = 0; //Create empty array for state-specific records $filtered = []; //Check every item's state foreach ($watchlist_items as $watch) { switch ($watch->state) { case 'active': $active++; break; case 'ended': $ended++; break; } if ($watch->state == $filter) { $filtered[] = $watch; } } if ($filter != 'all') { $watchlist['records'] = $filtered; } else { $watchlist['records'] = $watchlist_items; } //Pass on record counts $watchlist['active'] = $active; $watchlist['ended'] = $ended; $watchlist['all'] = $all; return $watchlist; }