Problem with query like

3

Good morning, I have this query that is like a search engine, only for products where the brand is "strong" .. in the parameter $ word, it can only bring a description of the product or code.

When I send a description of the product, the search engine works for me, but when I send a code it gives me the codes of other brands as a result, but I only need the strong brand.

public static function busqueda($palabra){

$producto = self::join('ped_unidades_medida','vcomp_productos_web1.unidad','=','ped_unidades_medida.unidad_uid')
    ->where('vcomp_productos_web1.marca','forte')
    ->where('vcomp_productos_web1.descripcion','like','%'.$palabra.'%')
    ->orwhere('vcomp_productos_web1.codigo','like','%'.$palabra.'%')
    ->select(['vcomp_productos_web1.*','ped_unidades_medida.unidad_descripcion'])
    ->where('vcomp_productos_web1.venta_web','SI')
    ->paginate(9);

    return $producto;

}

A result when I search by description

When I enter a code for a product that is not strong, it brings me this result, which is wrong ... I should only look for products where the brand is strong.

I leave here the SQL query. Suppose that $ word is 18

SELECT * FROM vcomp_productos_web1 
JOIN ped_unidades_medida ON vcomp_productos_web1.unidad = ped_unidades_medida.unidad_uid
WHERE vcomp_productos_web1.marca = 'FORTE'
AND vcomp_productos_web1.venta_web = 'SI'
AND vcomp_productos_web1.descripcion LIKE '%$palabra%'
OR vcomp_productos_web1.codigo LIKE '%$palabra%'
    
asked by RubxnMC 27.12.2017 в 14:03
source

2 answers

5

You must use a clause in the where in the following way:

->where(function ($query) use ($palabra) {
     $query->where('vcomp_productos_web1.descripcion','like','%'.$palabra.'%')
           ->orWhere('vcomp_productos_web1.codigo','like','%'.$palabra.'%');
    })

Remaining the code as follows:

public static function busqueda($palabra){

$producto = 
self::join('ped_unidades_medida','vcomp_productos_web1.unidad','=','ped_unidades_medida.unidad_uid')
        ->where('vcomp_productos_web1.marca','forte')
        ->where(function ($query) use ($palabra) {
                    $query->where('vcomp_productos_web1.descripcion','like','%'.$palabra.'%')
                          ->orWhere('vcomp_productos_web1.codigo','like','%'.$palabra.'%');
    })
    ->select(['vcomp_productos_web1.*','ped_unidades_medida.unidad_descripcion'])
    ->where('vcomp_productos_web1.venta_web','SI')
    ->paginate(9);

    return $producto;

You can review the documentation here

    
answered by 27.12.2017 / 14:26
source
3

The main problem is that the query is poorly armed. You must bear in mind that first solve the AND and then the OR , therefore your query:

SELECT * FROM vcomp_productos_web1 
JOIN ped_unidades_medida ON vcomp_productos_web1.unidad = ped_unidades_medida.unidad_uid
# resuelve primero esto, si es false entonces consulta por el 'codigo'
# evitando toda sub consulta anterior
WHERE (
  vcomp_productos_web1.marca = 'FORTE'
  AND vcomp_productos_web1.venta_web = 'SI'
  AND vcomp_productos_web1.descripcion LIKE '%$palabra%'
)
OR vcomp_productos_web1.codigo LIKE '%$palabra%'

What you should do is add a group to where of OR :

SELECT * FROM vcomp_productos_web1 
JOIN ped_unidades_medida ON vcomp_productos_web1.unidad = ped_unidades_medida.unidad_uid
# en este caso hace las comprobaciones de si la marca es 'FORTE'
# y si venta_web es 'SI' para luego buscar por la palabra en
# descripcion o codigo
WHERE
  vcomp_productos_web1.marca = 'FORTE'
  AND vcomp_productos_web1.venta_web = 'SI'
  AND (
    vcomp_productos_web1.descripcion LIKE '%$palabra%'
    OR vcomp_productos_web1.codigo LIKE '%$palabra%'
  )

In your code:

public static function busqueda($palabra){

$producto = self::join('ped_unidades_medida','vcomp_productos_web1.unidad','=','ped_unidades_medida.unidad_uid')
    ->where('vcomp_productos_web1.marca','forte')
    ->where(function($query) {
      $query->where('vcomp_productos_web1.descripcion','like','%'.$palabra.'%')
      $query->orwhere('vcomp_productos_web1.codigo','like','%'.$palabra.'%')
    })
    ->select(['vcomp_productos_web1.*','ped_unidades_medida.unidad_descripcion'])
    ->where('vcomp_productos_web1.venta_web','SI')
    ->paginate(9);

return $producto;
    
answered by 27.12.2017 в 14:52