/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { // get category in select field for filtering $category = $request->get('category'); // filter by catergoy if ($category && $category != 'All') { $threads = Thread::where('category', $category)->orderBy('created_at', 'desc')->get(); } else { $threads = Thread::orderBy('created_at', 'desc')->get(); } // Flash old input to repopulate on search $request->flash(); return view('threads.index', ['threads' => $threads]); }
public function popularThreads($page, $board) { $count = Thread::count(); $threads = Thread::orderBy(DB::raw("((30 - (DATEDIFF(NOW(), created_at))) / 30 * views)"), "DESC")->take(15); if ($board != 'all') { $threads->where('board', '=', $board); } $result = []; foreach ($threads->get() as $thread) { $threadArray = $thread->toArray(); $threadArray['post'] = $thread->posts()->take(1)->get()->toArray(); $result[] = $threadArray; } return response()->json(['total_rows' => $count, 'threads' => $result]); }