2
Follow the code snippet route/web.php
//Gerenciar UFs $this->group(['middleware' => ['auth'], 'namespace' =>'admin','prefix'=>'ufs'], function(){ //Inicio das Rotas de gerenciar os UFs $this->post('cadastro','UfController@cadastro')->name('uf.cadastro'); $this->post('uf','UfController@consulta')->name('uf.consulta'); $this->post('atualiza', 'UfController@atualiza')->name('uf.atualiza'); $this->post('remove','UfController@remove')->name('uf.remove'); $this->get('edita/{id}','UfController@edita')->name('uf.edita'); $this->get('novo','UfController@novo')->name('uf.novo'); $this->get('/uf','UfController@index')->name('admin.uf'); //Final das Rotas de gerenciar os UFs });
Follow the code Ufcontroller.php
namespace App\Http\Controllers\Admin; use Illuminate\Support\Facades\DB; use App\Models\UF; use Illuminate\Http\Request; use App\Http\Controllers\Controller; class UFController extends Controller { private $uf; //Construtor do UF public function __construct(UF $uf) { $this->uf = $uf; } public function index() { $ufs = UF::all(); $permissao = verificarPermissao('UF'); return view('admin.uf.index', ['ufs' => $ufs, 'permissoes' => $permissao]); } //Este método apresenta o formulário para cadastrar um novo UF public function novo() { return view('admin.uf.novo'); } //Método para cadastrar um UF public function cadastro(Request $request) { $uf = new UF(); $uf->uf_sigla = $request->get('sigla'); $uf->uf_descricao = $request->get('descricao'); $uf->ativo = $request->get('ativo'); $response = $uf->salvar($uf); if($response['success']){ return redirect() ->route('admin.uf') ->with('success',$response['message']); } else{ return redirect() ->back() ->with('error',$response['message']); } } //Método que redireciona para a página de atualizar o UF public function edita($id) { return view('admin.uf.edita', ['ufs' => UF::find($id)]); } //Este método remove a UF public function remove(Request $request) { $uf = UF::find($request->id); if (!$uf) return response() ->json(['error' => 'not_found'], 404); $response = $uf->deletar($uf); if($response['success']) { return response() ->json(['success' => $response['message']], 200); }else { // Caso não delete, informa um erro inesperado return redirect() ->json(['error' => $response['message']], 500); } } //Método de consulta da UF public function consulta(Request $request) { $condicoes = []; if($request->uf_sigla != null){ $condicoes[] = [DB::raw('upper(uf_sigla)'), 'like', DB::raw('upper("%'.$request->get('uf_sigla').'%")')]; } if($request->uf_descricao != null){ $condicoes[] = [DB::raw('upper(uf_descricao)'), 'like', DB::raw('upper("%'.$request->get('uf_descricao').'%")')]; } if($request->ativo != null){ $condicoes[] = ['ativo', '=', $request->get('ativo')]; } $request->flash(['uf_sigla','uf_descricao','ativo']); $ufs = UF::where($condicoes)->orderBy('uf_sigla')->get(); $permissao = verificarPermissao('UF'); return view('admin.uf.index', ['ufs' => $ufs, 'permissoes' => $permissao]); } //Método para atualiza o uf public function atualiza(Request $request) { $uf= UF::find($request->get('id')); $uf->uf_sigla = $request->get('sigla'); $uf->uf_descricao = $request->get('descricao'); $uf->ativo = $request->get('ativo'); $response = $uf->alterar($uf); if($response['success']) { return redirect() ->route('admin.uf') ->with('success',$response['message']); }else { return redirect() ->back() ->with('error',$response['message']); } } }
Change applied and successfully solved.
– Hugo Leonardo
@Hugoleonardo if friend’s answer solved your problem mark it as accepted" Click this icon
✔
next to the arrows of his answer.– hugocsl