【问题标题】:Laravel advanced search between product and relationshipsLaravel 产品和关系之间的高级搜索
【发布时间】:2023-03-31 06:30:01
【问题描述】:

我是 laravel 的新手,想使用 laravel 提供的关系行为。

我有一个搜索功能;

关键字, 类别, 媒体类型, 艺术家, 最低和最高价格

我的表格如下(只显示相关数据)

表格:产品(型号:产品)

id -> integer (primary)
name -> string
artist -> integer
active -> boolean

表:options_products(型号:ProductOptions)

id -> integer (primary)
products_id -> integer
price -> decimal(14,2)
active -> boolean

表格:类别(型号:类别)

id -> integer (primary)
parent_id -> integer
name -> string

表:category_products(模型 ProductCategories)

products_id -> integer
category_id -> integer

表格:media_type(模型:媒体)

id -> integer (primary)
name -> string

产品型号

public function categories() {
    return $this->belongsToMany('App\Category');
}

public function options() {
    return $this->hasMany('App\ProductOptions');
}

ProductOptions 模型:

public function mediaType()
{
    return $this->hasOne('App\Media', 'id', 'media');
}

public function product()
{
    return $this->hasOne('App\Products', 'id', 'products_id');
}

public function productsMany() {
    return $this->hasMany('App\Products', 'id', 'products_id');
}

ProductCategories 模型:

public function products()
{
    return $this->hasOne('App\Products', 'id', 'products_id');
}

类别模型:

public function products()
{
    return $this->belongsToMany('App\Products', 'category_products');
}

public function parent()
{
    return $this->belongsTo(self::class, 'parent_id');
}

public function children()
{
    return $this->hasMany(self::class, 'parent_id', 'id');
}

对于我的生活,我无法弄清楚如何将所有内容合并到搜索中,例如,如果他们选择了一个类别,以及最低和最高价格,因为该类别链接到产品表并且价格在 options_product桌子。基本上我需要它来搜索一个参数、几个或所有参数。我想我在搜索和搜索时不符合我的要求,除非我得到原始查询,否则找不到答案。

我目前有一个相当冗长的例程来处理大多数搜索选项,但是分开处理而不是最小和最大定价。例如对于文本搜索:

文本搜索:

if ($request->exists('search')) {
        /* filter & split */
        $search = Helpers::filter_search($request->get('search'));
        $products = Products::where(function ($q) use ($search) {
            foreach ($search as $value) {
                $q->orWhere('name', 'like', "%{$value}%");
                $q->orWhere('description', 'like', "%{$value}%");
            }
        })->where('active', 1)->paginate($views_per_page)->setPath('');
        $products->appends(array(
            'search' => $request->get('search')
        ));
    }

这是获取最小值/最大值的例程:

if ($request->exists('min') || $request->exists('max')) {
        $min = ($request->get('min')) ?? 0.00;
        $max = ($request->get('max')) ?? null;
        $products = Products::with('options')
            ->whereHas('options', function($q) use ($min,$max) {
                $q->where('options_products.price', '>=', $min);
                if ($max) {
                    $q->where('options_products.price', '<=', $max);
                }
            })->where('active', 1)->paginate($views_per_page);

    }

辅助函数:

public static function filter_search($query)
{
    $q = preg_split('/[;,+ ]+/', $query, -1, PREG_SPLIT_NO_EMPTY);
    $q = preg_replace("/[^ \w-]/", "", $q);
    return $q;
}

所以基本上,如果需要,我希望能够将所有这些都放在一个查询中。因此,如果他们的客户选择艺术家、类别、媒体、最低和/或最高价格以及文本搜索,它将创建一个完整的查询。对此的任何帮助将不胜感激,谢谢

【问题讨论】:

  • 这有点难以帮助,因为您没有确切的问题,但帮助我了解 eloquent 和查询构建器的是查看它构建的查询。将您的查询包装在 \DB::enableQueryLog()dd(\DB::getQueryLog()) 中,看看它给您带来了什么。
  • 我基本上是想将所有内容合并到一个查询中
  • 我只能找到一个查询。正在执行哪些查询?

标签: php laravel eloquent relationship


【解决方案1】:

搜索产品名称或具有类别名称的产品

 $products = Product::with('category')->where('name', 'LIKE', '%' . $data['name'] . '%')->orWhere( function($query) use ($data){
                $query->whereHas('category', function($q) use ($data){
                    $q->where('name', 'LIKE', '%' . $data['name'] . '%');
                });
            })->get();

这可以根据您的需要进行修改。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-18
    • 2016-01-01
    • 2015-04-17
    • 2012-04-26
    • 1970-01-01
    • 2020-10-24
    相关资源
    最近更新 更多