repository = $benefit; $this->repository ->pushCriteria(\Litepie\Repository\Criteria\RequestCriteria::class) ->pushCriteria(\Superrobota\Benefit\Repositories\Criteria\BenefitResourceCriteria::class); } /** * Display a list of benefit. * * @return Response */ public function index(BenefitRequest $request) { $view = $this->response->theme->listView(); if ($this->response->typeIs('json')) { $function = camel_case('get-' . $view); return $this->repository ->setPresenter(\Superrobota\Benefit\Repositories\Presenter\BenefitPresenter::class) ->$function(); } $benefits = $this->repository->paginate(); return $this->response->title(trans('benefit::benefit.names')) ->view('benefit::benefit.index', true) ->data(compact('benefits')) ->output(); } /** * Display benefit. * * @param Request $request * @param Model $benefit * * @return Response */ public function show(BenefitRequest $request, Benefit $benefit) { if ($benefit->exists) { $view = 'benefit::benefit.show'; } else { $view = 'benefit::benefit.new'; } return $this->response->title(trans('app.view') . ' ' . trans('benefit::benefit.name')) ->data(compact('benefit')) ->view($view, true) ->output(); } /** * Show the form for creating a new benefit. * * @param Request $request * * @return Response */ public function create(BenefitRequest $request) { $benefit = $this->repository->newInstance([]); return $this->response->title(trans('app.new') . ' ' . trans('benefit::benefit.name')) ->view('benefit::benefit.create', true) ->data(compact('benefit')) ->output(); } /** * Create new benefit. * * @param Request $request * * @return Response */ public function store(BenefitRequest $request) { try { $attributes = $request->all(); $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $benefit = $this->repository->create($attributes); return $this->response->message(trans('messages.success.created', ['Module' => trans('benefit::benefit.name')])) ->code(204) ->status('success') ->url(guard_url('benefit/benefit/' . $benefit->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/benefit/benefit')) ->redirect(); } } /** * Show benefit for editing. * * @param Request $request * @param Model $benefit * * @return Response */ public function edit(BenefitRequest $request, Benefit $benefit) { return $this->response->title(trans('app.edit') . ' ' . trans('benefit::benefit.name')) ->view('benefit::benefit.edit', true) ->data(compact('benefit')) ->output(); } /** * Update the benefit. * * @param Request $request * @param Model $benefit * * @return Response */ public function update(BenefitRequest $request, Benefit $benefit) { try { $attributes = $request->all(); $benefit->update($attributes); return $this->response->message(trans('messages.success.updated', ['Module' => trans('benefit::benefit.name')])) ->code(204) ->status('success') ->url(guard_url('benefit/benefit/' . $benefit->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('benefit/benefit/' . $benefit->getRouteKey())) ->redirect(); } } /** * Remove the benefit. * * @param Model $benefit * * @return Response */ public function destroy(BenefitRequest $request, Benefit $benefit) { try { $benefit->delete(); return $this->response->message(trans('messages.success.deleted', ['Module' => trans('benefit::benefit.name')])) ->code(202) ->status('success') ->url(guard_url('benefit/benefit/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('benefit/benefit/' . $benefit->getRouteKey())) ->redirect(); } } /** * Remove multiple benefit. * * @param Model $benefit * * @return Response */ public function delete(BenefitRequest $request, $type) { try { $ids = hashids_decode($request->input('ids')); if ($type == 'purge') { $this->repository->purge($ids); } else { $this->repository->delete($ids); } return $this->response->message(trans('messages.success.deleted', ['Module' => trans('benefit::benefit.name')])) ->status("success") ->code(202) ->url(guard_url('benefit/benefit')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/benefit/benefit')) ->redirect(); } } /** * Restore deleted benefits. * * @param Model $benefit * * @return Response */ public function restore(BenefitRequest $request) { try { $ids = hashids_decode($request->input('ids')); $this->repository->restore($ids); return $this->response->message(trans('messages.success.restore', ['Module' => trans('benefit::benefit.name')])) ->status("success") ->code(202) ->url(guard_url('/benefit/benefit')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->status("error") ->code(400) ->url(guard_url('/benefit/benefit/')) ->redirect(); } } }