public function index(Request $request) { $request->validate([ 'per_page' => ['integer', 'min:1'], 'sort_by' => ['string', 'in:question,answer'], 'sort_order' => ['string', 'in:asc,desc'], ]); $q = FAQ::query(); if ($request->q) { $searchTerm = $request->q; $q->where('question', 'like', "%{$searchTerm}%") ->orWhere('answer', 'like', "%{$searchTerm}%"); } $sortBy = $request->sort_by ?? 'question'; $sortOrder = $request->sort_order ?? 'asc'; $q->orderBy($sortBy, $sortOrder); $faqs = $q->paginate($request->per_page ?? 10); return FAQResource::collection($faqs); }
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter