public function search($input) { $query = Tag::query(); $columns = Schema::getColumnListing('tags'); $attributes = array(); foreach ($columns as $attribute) { if (isset($input[$attribute])) { $query->where($attribute, $input[$attribute]); $attributes[$attribute] = $input[$attribute]; } else { $attributes[$attribute] = null; } } return [$query->get(), $attributes]; }
/** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index(Request $request) { $this->validate($request, ['search' => 'string|max:20']); $query = Tag::query(); if ($request->has('search')) { $search = $request->input('search'); $query->whereHas('translations', function ($sub_query) use($search) { $sub_query->where('name', 'like', $search . '%'); }); } $query->orderBy('level', 'desc')->orderBy('id', 'desc'); $tags = $query->paginate(24); if ($request->wantsJSON()) { return response()->json($tags->toArray(), 200); } else { return view('pages.tag.index', ['tags' => $tags]); } }