German Burgos
German Burgos

Reputation: 1

Using Laravel how to do two requests to the database

I want to do two requests to the database, one of them is for a filter and another is to load data of a person. Both of them must to be in the same form(people.blade.php)

Controller:

public function cargarDatosCompra(){       
    $empleados = empleadoModel::all();
    $producto = insumosModel::all(); // distinct EN LARAVEL ???? no funca ->distinct();
    $datos = [$producto, $empleados];
    
     return view('compra',['datos' => $datos]);
}

public function buscarMarca(Request $request){
    
    $listaDeProductos = insumosModel::where('marca','LIKE','%'.$request->get('selectProveedor').'%', 'AND','categoria','LIKE','%'.$request->get('selectCategoria').'%' )->get();
    
    $empleados = empleadoModel::all();
    $producto = insumosModel::all();
    $datos = [$producto, $empleados];

    return view('compra',['datos' => $datos],['prodFiltrado' => $listaDeProductos]);

    

I try to do with two forms, both of them were using with method post

Upvotes: 0

Views: 61

Answers (1)

Donkarnash
Donkarnash

Reputation: 12835

Laravel query builder has a distinct() methods to pull records which are distinct. For the where condition involving check against two conditions with a logical AND you need to use parameter grouping by wrapping the where clauses within a where.

You can try

public function cargarDatosCompra(){       
    $empleados = empleadoModel::all();
    $producto = insumosModel::distinct()->get(); // distinct EN LARAVEL ???? no funca ->distinct();
    $datos = [$producto, $empleados];
    
     return view('compra',['producto' => $producto, 'empleados' => $empleados]);
}

public function buscarMarca(Request $request){
    
    $listaDeProductos = insumosModel::where(function($query){
        $query->where('marca','LIKE','%'.$request->get('selectProveedor').'%')
            ->where('categoria','LIKE','%'.$request->get('selectCategoria').'%');
    })->get();
    
    $empleados = empleadoModel::all();
    $producto = insumosModel::distinct()->get();
    $datos = [$producto, $empleados];

    return view('compra',[
        'empleados' => $empleados,
        'producto' => $producto,
        'prodFiltrado' => $listaDeProductos        
    ]);
}

Upvotes: 1

Related Questions