auth(__FUNCTION__); $opcoesChamada = $this->get(OpcaoChamada::class)->whereHas('centroCusto', function($query) { $query->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id); }) ->get() ->sortBy('centroCusto.setor.nome') ->values(); return response()->json($opcoesChamada, Response::HTTP_OK); } public function create() { $this->auth(__FUNCTION__); return response()->json( [ 'form' => [ 'setores' => CentroCusto::where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id) ->get() ->map(function($query) { return [ 'id' => $query->setor->id, 'centro_custo_id' => $query->id, 'nome' => $query->setor->nome, ]; })->sortBy('nome')->values(), 'telas' => Tela::select(['id', 'nome']) ->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id) ->orderBy('nome') ->get(), ], 'model' => [ 'id' => null, 'centroCustoId' => null, 'telaId' => null, ] ], Response::HTTP_OK ); } public function store(OpcaoChamadaRequest $request) { $this->auth(__FUNCTION__); return response()->json(OpcaoChamada::create($request->all())->fresh(), Response::HTTP_CREATED); } public function show($id) { $this->auth(__FUNCTION__); return response()->json($this->get(OpcaoChamada::class, $id), Response::HTTP_OK); } public function edit($id) { $this->auth(__FUNCTION__); $opcaoChamada = OpcaoChamada::findOrFail($id); return response()->json( [ 'form' => [ 'setores' => CentroCusto::where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id) ->get() ->map(function($query) { return [ 'id' => $query->setor->id, 'centro_custo_id' => $query->id, 'nome' => $query->setor->nome, ]; })->sortBy('nome')->values(), 'telas' => Tela::select(['id', 'nome']) ->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id) ->orderBy('nome') ->get(), ], 'model' => [ 'id' => $opcaoChamada->id, 'unidade_id' => $opcaoChamada->centroCusto->infraestrutura->unidade->id, 'infraestrutura_id' => $opcaoChamada->centroCusto->infraestrutura->id, 'centro_custo_id' => $opcaoChamada->centro_custo_id, 'tela_id' => $opcaoChamada->tela_id, ], ], Response::HTTP_OK ); } public function update(OpcaoChamadaRequest $request, $id) { $this->auth(__FUNCTION__); return response()->json(tap(OpcaoChamada::findOrFail($id))->update($request->all()), Response::HTTP_OK); } public function destroy($id) { $this->auth(__FUNCTION__); return response()->json(OpcaoChamada::findOrFail($id)->delete(), Response::HTTP_NO_CONTENT); } private function getCentroCustoLogado() { return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id')); } }