Laravel 搜索关系

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31871962/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:08:34  来源:igfitidea点击:

Laravel Search Relationship

phplaravelsearchrelation

提问by Hardist

I have two models which are related. I am trying to do a search in Products and only display the actual search results instead of ALL products of the category in which the product was found. I DO NOT want to search for any categories, since the categories will ALWAYS be displayed no matter what was searched for and no matter what was found.

我有两个相关的模型。我正在尝试在产品中进行搜索,并且只显示实际的搜索结果,而不是找到产品所在类别的所有产品。我不想搜索任何类别,因为无论搜索什么,无论找到什么,类别都将始终显示。

Example. I have the following categories:

- Food
- Drinks
- Candy

My "Food" category has the following products:

- Strawberry
- Apple
- Banana

My "Drinks" category has the following products:

- Banana Cocktail
- Beer
- Cola

My "Candy" category has the following products:

- Strawberry Lollipop
- Chocolate Bar
- Banana Ice Cream

So, what I WANT to achieve is the following. I do a search for a product called "Banana". What I WANT to be displayed is:

所以,我想要实现的是以下内容。我搜索了一种叫做“香蕉”的产品。我想要显示的是:

Category Food
- Product Banana

Category Drinks
- Product Banana Cocktail

Category Candy
- Product Banana Ice Cream

But my issue is, with my code, if I perform a search for "Banana", it displays the category in which banana is found, and it returns and displays ALL products in that category instead of ONLY the products that I searched for. How can I achieve it so only the products that was searched for are displayed?

但我的问题是,使用我的代码,如果我搜索“香蕉”,它会显示找到香蕉的类别,并返回并显示该类别中的所有产品,而不仅仅是我搜索的产品。我怎样才能实现它以便只显示搜索到的产品?

Categories Model:

类别型号:

class Categories extends Eloquent {

    public function products()
    {
        return $this->hasMany('Products');
    } 
}

Products Model:

产品型号:

class Products extends Eloquent {

    public function categories()
    {
        return $this->belongsTo('Categories');
    }
}

My Controller:

我的控制器:

    $searchString       = Input::get('search');

    if($searchString)
    {
        $categories = Categories::with('products')->orderBy($order, $by)->whereHas('products', function ($query) use ($searchString){
            $query->where('name', 'like', '%'.$searchString.'%');
        })->get();
    }
    else {
        $categories     = Categories::with('products')->orderBy($order, $by)->get();
    }

My View:

我的看法:

@foreach($categories as $category)
    {{ $category->name }} // Show the category name

    @foreach($category->products as $product)
    {{ $product->name }} // Show all products in that category

    @endforeach
@endforeach

回答by Pawel Bieszczad

I don't know what how are you viewing the results, but I think if you just eager load the products on the categories you should be good.

我不知道您如何看待结果,但我认为如果您只是急切地将产品加载到类别上,您应该会很好。

$categories = Categories::whereHas('products', function ($query) use ($searchString){
        $query->where('name', 'like', '%'.$searchString.'%');
    })
    ->with(['products' => function($query) use ($searchString){
        $query->where('name', 'like', '%'.$searchString.'%');
    }])->get();

foreach($categories as $category){
    echo $category->name . ':' . PHP_EOL;
    foreach($category->products as $product){
        echo . '-' . $product->name . PHP_EOL;
    }
}