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