You can not select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
					
					
						
							111 lines
						
					
					
						
							3.5 KiB
						
					
					
				
			
		
		
		
			
			
			
				
					
				
				
					
				
			
		
		
	
	
							111 lines
						
					
					
						
							3.5 KiB
						
					
					
				| <?php | |
| 
 | |
| namespace App\Http\Controllers; | |
| 
 | |
| use App\Models\tiposVeiculos; | |
| use Illuminate\Http\Request; | |
| 
 | |
| class TiposVeiculosController extends Controller | |
| { | |
|     /** | |
|      * Display a listing of the resource. | |
|      */ | |
|     public function index(Request $request) | |
|     { | |
|         $busqueda = $request->busqueda; | |
| 
 | |
|         if($busqueda) { | |
|             $tiposVeiculos = tiposVeiculos::where('nombre', 'LIKE', "%{$busqueda}%")->where('status', true)->get(); | |
| 
 | |
|             if($tiposVeiculos->count() == 0) { | |
|                 return redirect()->route('tiposVeiculos.index') | |
|                                ->with('error', value: 'No existe ningun tipo de vehiculo con el nombre "' . $busqueda . '". Por favor, inténtalo de nuevo.'); | |
|             } | |
| 
 | |
|             // Si solo hay una marca, mostrar sus detalles | |
|             if($tiposVeiculos->count() == 1) { | |
|                 $tiposVeiculos = $tiposVeiculos->first(); | |
|                 return redirect()->route('vehiculos.edit', $tiposVeiculos->id); | |
|             } | |
| 
 | |
|             // Si hay múltiples coincidencias, mostrar la lista filtrada | |
|             return view('vehiculos', ["tiposVeiculos" => $tiposVeiculos]); | |
|         } | |
| 
 | |
|             // Si no hay búsqueda, mostrar todas las marcas | |
|             $tiposVeiculos = tiposVeiculos::where('status', true)->get(); | |
|             return view('vehiculos', ["tiposVeiculos" => $tiposVeiculos]); | |
|     } | |
| 
 | |
|     /** | |
|      * Show the form for creating a new resource. | |
|      */ | |
|     public function create() | |
|     { | |
|         $tiposVeiculos = tiposVeiculos::all(); | |
|         return view('vehiculosCrearEditar',['tiposVeiculos'=>$tiposVeiculos]); | |
|     } | |
| 
 | |
|     /** | |
|      * Store a newly created resource in storage. | |
|      */ | |
|     public function store(Request $request) | |
|     { | |
|         $tiposVeiculos = new tiposVeiculos($request->all()); | |
|         $tiposVeiculos->status = true; | |
|         $tiposVeiculos->save(); | |
|         return redirect()->route('vehiculos.index')->with('success', 'Tipo de vehiculo creado exitosamente.'); | |
|     } | |
| 
 | |
|     /** | |
|      * Display the specified resource. | |
|      */ | |
|     public function show(tiposVeiculos $tiposVeiculos) | |
|     { | |
|         // | |
|     } | |
| 
 | |
|     /** | |
|      * Show the form for editing the specified resource. | |
|      */ | |
|     public function edit($id) | |
|     { | |
|         $tipoVehiculo = tiposVeiculos::find($id); | |
|         return view('vehiculosCrearEditar', ['tipoVehiculo' => $tipoVehiculo]); | |
|     } | |
| 
 | |
|     /** | |
|      * Update the specified resource in storage. | |
|      */ | |
|     public function update(Request $request, $id) | |
|     { | |
|         $tipoVehiculo = tiposVeiculos::find($id); | |
|         $tipoVehiculo->fill($request->all()); | |
|         if ($request->has('status')) { | |
|             $tipoVehiculo->status = $request->status; | |
|         } | |
|         $tipoVehiculo->save(); | |
|         return redirect()->route('vehiculos.index')->with('success', 'Vehículo actualizado exitosamente.'); | |
|     } | |
| 
 | |
|     public function toggleStatus($id) | |
|     { | |
|         $tipoVehiculo = tiposVeiculos::findOrFail($id); | |
|         $tipoVehiculo->status = !$tipoVehiculo->status; | |
|         $tipoVehiculo->save(); | |
| 
 | |
|         $mensaje = $tipoVehiculo->status ? 'Tipo de vehículo activado exitosamente.' : 'Tipo de vehículo desactivado exitosamente.'; | |
|         return redirect()->route('vehiculos.index')->with('success', $mensaje); | |
|     } | |
| 
 | |
|     /** | |
|      * Remove the specified resource from storage. | |
|      */ | |
|     public function destroy($id) | |
|     { | |
|         $tiposVeiculos = tiposVeiculos::find($id); | |
|         $tiposVeiculos->status = false; | |
|         $tiposVeiculos->save(); | |
|         return redirect()->route('vehiculos.index')->with('success', 'Vehiculo eliminado exitosamente.'); | |
|     } | |
| }
 | |
| 
 |