/** * @api {get} /stickers/search Search Stickers and Tasks * @apiGroup Stickers * @apiDescription Find stickers and tasks * * @param Request $request * * @return \Illuminate\Http\Response */ public function search(Request $request) { $term = $request->input('q'); $query = DB::select('SELECT * FROM stickers WHERE name LIKE ? AND deletedAt IS NULL', ["%{$term}%"]); /** @var Sticker[] $stickers */ $stickers = Sticker::hydrate($query); $query = DB::select('SELECT * FROM tasks WHERE name LIKE ? AND deletedAt IS NULL', ["%{$term}%"]); /** @var Task[] $tasks */ $tasks = Task::hydrate($query); $results = []; foreach ($stickers as $sticker) { $results[] = ['type' => 'sticker', 'people' => $sticker->peopleCount, 'sticker' => $sticker]; } foreach ($tasks as $task) { $results[] = ['type' => 'task', 'task' => $task, 'people' => $task->peopleCount, 'sticker' => $task->getSticker()]; } usort($results, function ($a, $b) { $countA = $a['type'] === 'task' ? $a['task']->peopleCount : $a['sticker']->peopleCount; $countB = $b['type'] === 'task' ? $b['task']->peopleCount : $b['sticker']->peopleCount; return $countA < $countB; }); return $this->response(['results' => $results]); }