auth(__FUNCTION__); $where = $this->getWhereFromQueryString([ 'centro_custo_id_de' ]); $encaminhamentos = $this->get(Encaminhamento::class) ->where($where) ->whereHas('centroCustoDe', function($query) { $query->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id); }) ->whereHas('centroCustoPara', function($query) { $query->where('infraestrutura_id', $this->getCentroCustoLogado()->infraestrutura_id); }) ->get() ->sortBy('centroCustoDe.setor.nome') ->values(); return response()->json($encaminhamentos, 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(), ], 'model' => [ 'id' => null, 'centroCustoIdDe' => null, 'centroCustoIdPara' => null, ] ], Response::HTTP_OK ); } public function store(EncaminhamentoRequest $request) { $this->auth(__FUNCTION__); return response()->json(Encaminhamento::create($request->all())->fresh(), Response::HTTP_CREATED); } public function show($id) { $this->auth(__FUNCTION__); return response()->json($this->get(Encaminhamento::class, $id), Response::HTTP_OK); } public function edit($id) { $this->auth(__FUNCTION__); $encaminhamento = Encaminhamento::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(), ], 'model' => [ 'id' => $encaminhamento->id, 'centro_custo_id_de' => $encaminhamento->centro_custo_id_de, 'centro_custo_id_para' => $encaminhamento->centro_custo_id_para, ], ], Response::HTTP_OK ); } public function update(EncaminhamentoRequest $request, $id) { $this->auth(__FUNCTION__); return response()->json(tap(Encaminhamento::findOrFail($id))->update($request->all()), Response::HTTP_OK); } public function destroy($id) { $this->auth(__FUNCTION__); return response()->json(Encaminhamento::findOrFail($id)->delete(), Response::HTTP_NO_CONTENT); } private function getCentroCustoLogado() { return CentroCusto::findOrFail(auth()->payload()->get('centro_custo_id')); } }