Laravel - 查询模型如果值包含某个字符串(取自搜索输入) [英] Laravel - Query Model if values contain a certain string (taken from search input)

查看:2551
本文介绍了Laravel - 查询模型如果值包含某个字符串(取自搜索输入)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel和Ajax进行搜索。所以我有一个属于标签和子类别的产品。另一方面,子类别属于一个类别。我想检查他们的所有属性(字段值),并检查它们是否包含给定的字符串。有一些搜索,我发现我必须使用 LIKE 。这是我试过的:

  $ products = Product :: where('name_en','LIKE',$ search) - >得到(); 

但是,如果搜索字符串完全符合该值,则会获得该产品。我想匹配,如果它包含它。如何处理belongsTo关系?如何检查标签和子类别的优点?如何将所有东西连在一起,从而达到预期的效果?感谢提前。

解决方案

你做错了一个事情,你的查询返回你完全匹配,因为你给出了确切的字符串。但是您的查询应该是这样的。

  $ products = Product :: where('name_en','LIKE','% 。$搜索。 '%') - >获得(); 

以上查询将给您的包含搜索字符串的产品。



如果要在关系表中搜索,则可以使用用户laravel方法 join()。但是还有一种方法 whereHas ,但是我总是避免使用这种方法,因为它创建了非常复杂的查询。这很重所以你可以使用 join()方法,它将使用关系表添加 inner join



这是加入的例子:

  $ products = Product :: join('tags' ,function($ builder){
$ builder-> on('tags.id','=','products.tag_id');
//这里你可以在标签表上添加更多条件。
})
join('sub_categories',function($ builder){
$ builder-> on('sub_categories.id','=','products.tag_id') ;
//这里你可以在子类别表上添加更多的条件
})
- > where('name_en','LIKE','%'。$ search。'%' )
- > get();

这是一个基本的例子,你可以根据你的要求使用这个。


I am implementing a search using Laravel and Ajax. So I have a Product which belongs to a Tag and a Subcategory. On the other hand the Subcategory belongs to a Category. I want to check all of their properties (field values) and check if they contain the given string. With some searching I found out that I have to use LIKE. Here is what I tried:

$products = Product::where('name_en', 'LIKE', $search)->get();

However this will get the products if the search string matches exactly the value. I want to match if it contains it. How can I proceed with the belongsTo relationships? How can I check the propreties of Tag and Subcategory as well? How to chain everything together so I achieve the desired result? Thanks in advance.

解决方案

you are doing one thing wrong, your query returns you exact matches because you given the exact string. But your query should be like this.

$products = Product::where('name_en', 'LIKE', '%'.$search.'%')->get();

Above query will gives your products which contains the searched string.

And if you want to search in relational tables then you can user laravel method join(). But there are one more method whereHas but I always avoiding this method, because it creates very complex query. which is very heavy. So you can use join() method which will add inner join with relational table.

Here is the example of join:

$products = Product::join('tags', function($builder) {
                        $builder->on('tags.id', '=', 'products.tag_id');
                        // here you can add more conditions on tags table.
                    })
                    join('sub_categories', function($builder) {
                        $builder->on('sub_categories.id', '=', 'products.tag_id');
                        // here you can add more conditions on subcategories table.
                    })
                    ->where('name_en', 'LIKE', '%'.$search.'%')
                    ->get();

This is the basic example, you can use this according to your requirement.

这篇关于Laravel - 查询模型如果值包含某个字符串(取自搜索输入)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆