Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

179
Visualizações
Laravel Search Keyword Query

So currently I've created search text field to find keywords for specific columns of my database.

Using this eloquent

$data = DB::table('product')
        ->select('product.id','product.title','product.description','product.content','product.photo','product.quantity','product.price')
        ->join('product_per_category','product_per_category.product_id','=','product.id')
        ->where(['product.deleted' => 0])
        ->where(['product_per_category.deleted' => 0])
        ->where(['product_per_category.productcategory_id' => $id])
        ->where(function($query) use ($keyword){
            $query->where('product.content', 'like', '%' . $keyword . '%')
                ->orWhere('product.title', 'like', '%' . $keyword . '%')
                ->orWhere('product.quantity', 'like', '%' . $keyword . '%')
                ->orWhere('product.price', 'like', '%' . $keyword . '%');
        })
        ->groupBy('product.id')
        ->paginate(10);

If I try to search 234.00 value of Price all possible value that will match with the columns of content, title, quantity, price, quantity will automatically return the data.

How ever if I tried to search for a keyword like this

234.00 Books value of Price and Title. it will not return any data now. Because it will match only this sentence on specific columns.

What I'm trying to do is if I search something like this

= 234.00 Books 20

*Value of Price, Title and Quantity

It should return all the data that will match on every columns. Whether the letters lowercase or uppercase as long as it matches the position of letters

Is that possible?

** UPDATE **

Objective : Just want to match the 234 value on any columns. Be the result. But the problem of my query is, if I add another keyword like 234 Books it doesnt return any data

over 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

First: Extract the keywords:

$delimeter = ' '; //or your separator
$keywords = explode($delimeter, $search); // Will return an array containing each keyword

Then: Loop over each keyword and call your search query or expand your query dynamically

Example Something like this should work...

$base_query = DB::table('product')
        ->select('product.id','product.title','product.description','product.content','product.photo','product.quantity','product.price')
        ->join('product_per_category','product_per_category.product_id','=','product.id')
        ->where(['product.deleted' => 0])
        ->where(['product_per_category.deleted' => 0])
        ->where(['product_per_category.productcategory_id' => $id]);

foreach($keywords as $keyword){
       $base_query->where(function($query) use ($keyword){
            $query->where('product.content', 'like', '%' . $keyword . '%')
                ->orWhere('product.title', 'like', '%' . $keyword . '%')
                ->orWhere('product.quantity', 'like', '%' . $keyword . '%')
                ->orWhere('product.price', 'like', '%' . $keyword . '%');
            })
}

$result = $base_query->get();

You will need to do a loop because you don't know the amount of keywords in your search result

over 4 years ago · Santiago Trujillo Relatório

0

You can use explode PHP function like this:

$keywords = explode(' ', $keyword); // Will return an array containing each keyword
dd($keywords);

If your keywords can contain spaces, you may should consider using another delimiter or splitting your query parameters.

over 4 years ago · Santiago Trujillo Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda