repository = $country; parent::__construct(); } /** * Display a list of country. * * @return Response */ public function index(CountryRequest $request) { if ($request->wantsJson()) { return $this->getJson($request); } $this ->theme->prependTitle(trans('country::country.names')); return $this->theme->of('country::admin.country.index')->render(); } /** * Display a list of country. * * @return Response */ public function getJson(CountryRequest $request) { $pageLimit = $request->input('pageLimit'); $countries = $this->repository ->pushCriteria(app('Litepie\Repository\Criteria\RequestCriteria')) ->setPresenter('\\Country\\Country\\Repositories\\Presenter\\CountryListPresenter') ->scopeQuery(function($query){ return $query->orderBy('id','DESC'); })->paginate($pageLimit); $countries['recordsTotal'] = $countries['meta']['pagination']['total']; $countries['recordsFiltered'] = $countries['meta']['pagination']['total']; $countries['request'] = $request->all(); return response()->json($countries, 200); } /** * Display country. * * @param Request $request * @param Model $country * * @return Response */ public function show(CountryRequest $request, Country $country) { if (!$country->exists) { return response()->view('country::admin.country.new', compact('country')); } Form::populate($country); return response()->view('country::admin.country.show', compact('country')); } /** * Show the form for creating a new country. * * @param Request $request * * @return Response */ public function create(CountryRequest $request) { $country = $this->repository->newInstance([]); Form::populate($country); return response()->view('country::admin.country.create', compact('country')); } /** * Create new country. * * @param Request $request * * @return Response */ public function store(CountryRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id('admin.web'); $country = $this->repository->create($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('country::country.name')]), 'code' => 204, 'redirect' => trans_url('/admin/country/country/'.$country->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, ], 400); } } /** * Show country for editing. * * @param Request $request * @param Model $country * * @return Response */ public function edit(CountryRequest $request, Country $country) { Form::populate($country); return response()->view('country::admin.country.edit', compact('country')); } /** * Update the country. * * @param Request $request * @param Model $country * * @return Response */ public function update(CountryRequest $request, Country $country) { try { $attributes = $request->all(); $country->update($attributes); return response()->json([ 'message' => trans('messages.success.updated', ['Module' => trans('country::country.name')]), 'code' => 204, 'redirect' => trans_url('/admin/country/country/'.$country->getRouteKey()) ], 201); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/country/country/'.$country->getRouteKey()), ], 400); } } /** * Remove the country. * * @param Model $country * * @return Response */ public function destroy(CountryRequest $request, Country $country) { try { $t = $country->delete(); return response()->json([ 'message' => trans('messages.success.deleted', ['Module' => trans('country::country.name')]), 'code' => 202, 'redirect' => trans_url('/admin/country/country/0'), ], 202); } catch (Exception $e) { return response()->json([ 'message' => $e->getMessage(), 'code' => 400, 'redirect' => trans_url('/admin/country/country/'.$country->getRouteKey()), ], 400); } } }