form = ProductForm::grouped(false) ->setAttributes() ->toArray(); $this->modules = $this->modules(config('lavalite.product.modules'), 'product', guard_url('product')); } /** * Display a list of product. * * @return Response */ public function index(ProductRequest $request) { $pageLimit = $request->input('pageLimit', config('database.pagination.limit')); $page = Product::pushScope(new RequestScope()) ->pushScope(new ProductResourceScope()) ->simplePaginate($pageLimit); $data = new ProductsCollection($page); $form = $this->form; $modules = $this->modules; return $this->response->setMetaTitle(trans('product::product.names')) ->view('product::product.index') ->data(compact('data', 'modules', 'form')) ->output(); } /** * Display product. * * @param Request $request * @param Model $product * * @return Response */ public function show(ProductRequest $request, Product $model) { $form = $this->form; $modules = $this->modules; $data = new ProductResource($model); return $this->response ->setMetaTitle(trans('app.view') . ' ' . trans('product::product.name')) ->data(compact('data', 'form', 'modules')) ->view('product::product.show') ->output(); } /** * Show the form for creating a new product. * * @param Request $request * * @return Response */ public function create(ProductRequest $request, Product $model) { $form = $this->form; $modules = $this->modules; $data = new ProductResource($model); return $this->response->setMetaTitle(trans('app.new') . ' ' . trans('product::product.name')) ->view('product::product.create') ->data(compact('data', 'form', 'modules')) ->output(); } /** * Create new product. * * @param Request $request * * @return Response */ public function store(ProductRequest $request, Product $model) { try { $attributes = $request->all(); $attributes['user_id'] = user_id(); $attributes['user_type'] = user_type(); $model = $model->create($attributes); $data = new ProductResource($model); return $this->response->message(trans('messages.success.created', ['Module' => trans('product::product.name')])) ->code(204) ->data(compact('data')) ->status('success') ->url(guard_url('product/product/' . $model->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('/product/product')) ->redirect(); } } /** * Show product for editing. * * @param Request $request * @param Model $product * * @return Response */ public function edit(ProductRequest $request, Product $model) { $form = $this->form; $modules = $this->modules; $data = new ProductResource($model); return $this->response->setMetaTitle(trans('app.edit') . ' ' . trans('product::product.name')) ->view('product::product.edit') ->data(compact('data', 'form', 'modules')) ->output(); } /** * Update the product. * * @param Request $request * @param Model $product * * @return Response */ public function update(ProductRequest $request, Product $model) { try { $attributes = $request->all(); $model = $model->update($attributes); $data = new ProductResource($model); return $this->response->message(trans('messages.success.updated', ['Module' => trans('product::product.name')])) ->code(204) ->status('success') ->data(compact('data')) ->url(guard_url('product/product/' . $model->getRouteKey())) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('product/product/' . $model->getRouteKey())) ->redirect(); } } /** * Remove the product. * * @param Model $product * * @return Response */ public function destroy(ProductRequest $request, Product $model) { try { $model->delete(); $data = new ProductResource($model); return $this->response->message(trans('messages.success.deleted', ['Module' => trans('product::product.name')])) ->code(202) ->status('success') ->data(compact('data')) ->url(guard_url('product/product/0')) ->redirect(); } catch (Exception $e) { return $this->response->message($e->getMessage()) ->code(400) ->status('error') ->url(guard_url('product/product/' . $model->getRouteKey())) ->redirect(); } } }