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.
		
		
		
		
			
				
					98 lines
				
				2.4 KiB
			
		
		
			
		
	
	
					98 lines
				
				2.4 KiB
			| 
								 
											8 months ago
										 
									 | 
							
								<?php
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								namespace App\Http\Controllers;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								use App\Models\Marca;
							 | 
						||
| 
								 | 
							
								use Illuminate\Http\Request;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								class MarcaController extends Controller
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Display a listing of the resource.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								    public function index(Request $request)
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								    {
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								        $busqueda = $request->busqueda;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if($busqueda) {
							 | 
						||
| 
								 | 
							
								        $marcas = Marca::where('nombre', 'LIKE', "%{$busqueda}%")->get();
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if($marcas->count() == 0) {
							 | 
						||
| 
								 | 
							
								            return redirect()->route('marca.index')
							 | 
						||
| 
								 | 
							
								                           ->with('error', 'No existe ninguna marca con el nombre "' . $busqueda . '". Por favor, inténtalo de nuevo.');
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // Si solo hay una marca, mostrar sus detalles
							 | 
						||
| 
								 | 
							
								        if($marcas->count() == 1) {
							 | 
						||
| 
								 | 
							
								            $marca = $marcas->first();
							 | 
						||
| 
								 | 
							
								            return redirect()->route('marca.edit', $marca->id);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // Si hay múltiples coincidencias, mostrar la lista filtrada
							 | 
						||
| 
								 | 
							
								        return view('marca', ["marcas" => $marcas]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        // Si no hay búsqueda, mostrar todas las marcas
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								        $marcas = Marca::all();
							 | 
						||
| 
								 | 
							
								        return view('marcas', ["marcas" => $marcas]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Show the form for creating a new resource.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function create()
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $marcas = Marca::all();
							 | 
						||
| 
								 | 
							
								        return view('marcasCrearEditar',['marcas'=>$marcas]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Store a newly created resource in storage.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function store(Request $request)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $marca = new Marca($request->all());
							 | 
						||
| 
								 | 
							
								        $marca->save();
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								        return redirect()->route('marca.index')->with('success', 'Marca creada exitosamente.');
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Display the specified resource.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function show(Marca $marca)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        //
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Show the form for editing the specified resource.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function edit($id)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $marca = Marca::find($id);
							 | 
						||
| 
								 | 
							
								        return view('marcasCrearEditar',['marca'=>$marca]);
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Update the specified resource in storage.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    public function update(Request $request, $id)
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        $marca = Marca::find($id);
							 | 
						||
| 
								 | 
							
								        $marca->fill($request->all());
							 | 
						||
| 
								 | 
							
								        $marca->save();
							 | 
						||
| 
								 | 
							
								        return redirect()->route('marca.index');
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /**
							 | 
						||
| 
								 | 
							
								     * Remove the specified resource from storage.
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								    public function destroy($id )
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								    {
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								        $marca = Marca::find($id);
							 | 
						||
| 
								 | 
							
								        $marca->delete();
							 | 
						||
| 
								 | 
							
								        return redirect()->route('marca.index')->with('success', 'Marca eliminada exitosamente.');
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 
											8 months ago
										 
									 | 
							
								    }
							 | 
						||
| 
								 | 
							
								}
							 |