of($unidadesExterna)->toJson(); } public function index() { $this->auth(__FUNCTION__); $unidades = $this->get(UnidadeExterna::class)->orderBy('nome_fantasia')->get(); return response()->json($unidades, Response::HTTP_OK); } public function store(UnidadeExternaRequest $request) { $this->auth(__FUNCTION__); $params = $this->getParams($request); $unidadeExterna = UnidadeExterna::where('nome_fantasia', $params['nome_fantasia'])->first(); if (!is_null($unidadeExterna)) { throw new DuplicatedNomeFantasiaException; } return response()->json(UnidadeExterna::create($params)->fresh(), Response::HTTP_CREATED); } public function show($id) { $this->auth(__FUNCTION__); return response()->json($this->get(UnidadeExterna::class, $id), Response::HTTP_OK); } public function edit($id) { $this->auth(__FUNCTION__); $unidadeExterna = UnidadeExterna::findOrFail($id); return response()->json( [ 'form' => [], 'model' => $unidadeExterna ], Response::HTTP_OK ); } public function update(UnidadeExternaRequest $request, $id) { $this->auth(__FUNCTION__); $params = $this->getParams($request); $unidadeExterna = UnidadeExterna::where('nome_fantasia', $params['nome_fantasia']) ->where('id', '<>', $id) ->first(); if (!is_null($unidadeExterna)) { throw new DuplicatedNomeFantasiaException; } $unidade = tap(UnidadeExterna::findOrFail($id))->update($params); return response()->json($unidade, Response::HTTP_OK); } private function getParams($request) { return [ 'nome_fantasia' => mb_strtoupper($request->nome_fantasia), 'municipio_id' => $request->municipio_id, ]; } public function destroy($id) { $this->auth(__FUNCTION__); UnidadeExterna::findOrFail($id)->delete(); return response()->json(null, Response::HTTP_NO_CONTENT); } public function datatable() { $unidadesExterna = UnidadeExterna::with([]); return datatables()->of($unidadesExterna)->toJson(); } }