/** * Execute the console command. * * @return mixed */ public function handle() { $mangaCount = Manga::count(); $offset = 0; $limit = 1000; $this->output->progressStart($mangaCount); DB::beginTransaction(); DB::table('manga_category')->delete(); while ($offset < $mangaCount) { $mangas = Manga::select(['id', 'cat'])->orderBy('id', 'asc')->skip($offset)->take($limit)->get(); foreach ($mangas as $manga) { foreach ($manga->cat as $catId) { if ($catId != '') { $mangaCategory = new MangaCategory(); $mangaCategory->mng_id = $manga->id; $mangaCategory->cat_id = $catId; $mangaCategory->save(); } } $this->output->progressAdvance(); } $offset += $limit; } DB::commit(); $this->output->progressFinish(); }
/** * Display a listing of the resource. * * @return Response */ public function index(Request $request) { $search = $request->input('search'); $categories = $request->input('categories'); $status = $request->input('status'); $sort = $request->input('sort'); if (!in_array($sort, ['nme', 'rnk', 'rnk_wek', 'dte_upd'])) { $sort = 'rnk'; } $order = $request->input('order'); if (!in_array($order, ['asc', 'desc'])) { $order = 'asc'; } $offset = $request->input('offset', 0); $limit = min($request->input('limit', static::MAX_COUNT_PER_REQUEST), static::MAX_COUNT_PER_REQUEST); $mangaIds = []; if (!is_null($categories)) { if (!is_array($categories)) { $categories = [$categories]; } $results = MangaCategory::select('mng_id')->whereIn('cat_id', $categories)->distinct()->get(); foreach ($results as $result) { $mangaIds[] = $result->mng_id; } } $query = $this->makeMangaIndexQuery($mangaIds, $status, $search); $mangaTotalCount = $query->count(); $mangas = $this->makeMangaIndexQuery($mangaIds, $status, $search)->select(Manga::$briefAttrToSelect)->orderBy($sort, $order)->skip($offset)->take($limit)->get(); return response()->json($mangas->map(function ($manga) { return $manga->toArray(Manga::$briefAttrToOutput); }))->header('X-Total-Count', $mangaTotalCount); }