Laravel搜索关系 [英] Laravel Search Relationship

查看:73
本文介绍了Laravel搜索关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

因此,我想实现以下目标.我搜索名为香蕉"的产品.我想显示的是:

Category Food
- Product Banana

Category Drinks
- Product Banana Cocktail

Category Candy
- Product Banana Ice Cream

但是我的问题是,使用我的代码,如果我执行香蕉"搜索,它将显示找到香蕉的类别,然后返回并显示该类别中的所有产品,而不是仅显示我搜索过的产品为了.如何实现它,以便仅显示搜索到的产品?

类别模型:

class Categories extends Eloquent {

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

产品型号:

class Products extends Eloquent {

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

我的控制器:

    $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();
    }

我的观点:

@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

解决方案

我不知道您如何看待结果,但我认为,如果您只是渴望将产品加载到类别中,那您应该会很好.

>

$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;
    }
}

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

解决方案

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;
    }
}

这篇关于Laravel搜索关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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