我有两个相关的型号.我试图在产品中进行搜索,并且只显示实际的搜索结果,而不是找到该产品所在类别的所有产品.我不想搜索任何类别,因为无论搜索什么,无论找到什么,这些类别都会一直显示出来.

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?

类别型号:

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

推荐答案

我不知道你是如何看待结果的,但我认为如果你只是Eager 地将产品加载到分类中,你应该会很好.

$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相关问答推荐

如何更改LIVE上的Filament占位符内容?

运行NPM Prod时出现VUE问题

Uncaught ReferenceError:未定义require[Shopify PHP React Browser问题]

Laravel 10 - 没有 $append 属性的Eloquent 的热切加载

如何在 Laravel 中使用 return 停止 Trait php 的执行

如何在 Laravel 中使用 Vue 路由?

如何通过单击进入 vs 代码中的类

Laravel 5 - 手动分页

Lumen和 MongoDB?

查询 Laravel Select WhereIn 数组

Laravel 的 APP_ENV 变量(在 .env 文件中找到)是否有建议的值列表?

laravel 中的 Http Post 使用 fetch api 给出 TokenMismatchException

无法安装 Laravel 包 - 干预图像

laravel 预期响应代码 250 但得到代码530

在 laravel 分页中添加一些数据

Laravel 5:完整性约束违规:1452 无法添加或更新子行:外键约束失败

在 Laravel 的 Homestead 中运行 PHPUnit

我如何从给定的日期时间 struct 创建Carbon 对象?

如何清理 Laravel Bootstrap 缓存配置文件?

使用 Laravel Mix 合并多个文件