Php 多行雄辩查询

Php 多行雄辩查询,php,laravel,eloquent,Php,Laravel,Eloquent,我正在尝试根据所选的过滤器和可能的搜索词/词筛选我的产品。我的过滤器与类别有关系,而类别又与我的产品有关系。我下面的代码只有在所有内容都链接在一起时才起作用(没有if语句检查搜索词/词),但当我尝试将查询拆分为多行时(我已经读过了,是吗?),它返回一个空数组 以下是我的代码: // Create array from selected categories/filters $filter_ids = explode(',', $request->get('cats')); // Quer

我正在尝试根据所选的过滤器和可能的搜索词/词筛选我的产品。我的过滤器与类别有关系,而类别又与我的产品有关系。我下面的代码只有在所有内容都链接在一起时才起作用(没有if语句检查搜索词/词),但当我尝试将查询拆分为多行时(我已经读过了,是吗?),它返回一个空数组

以下是我的代码:

// Create array from selected categories/filters
$filter_ids = explode(',', $request->get('cats'));

// Query for active products
$products = Product::where('active', '=', 1);
$products->with(['categories' => function($query) use ($filter_ids) {
    // Query for active categories
    $query->where('active', 1)->whereHas('filters', function ($query)  use ($filter_ids) {
        // Query for the selected filters from the request 
        $query->whereIn('id', $filter_ids);
    });
}]);

// Check for search term/word
if ($request->get('q')) {
    $q = $request->get('q') ? urldecode($request->get('q')) : null;
    $products->where('title', 'LIKE', "%{$q}%");
}

// Limit to 10 items and get results
$products->limit(10)->get();

return response()->json([
    'status' => 'success',
    'response' => $products
], 200);

我认为您可以但是在添加关系之前,不需要先查询所有有标题的产品。但是这里的错误在于,在将get()的结果添加到json响应主体之前,必须将其存储在变量中:

试着做一些类似的事情:

if ($request->get('q')) {
$q = $request->get('q') ? urldecode($request->get('q')) : null;
$products->where('title', 'LIKE', "%{$q}%");
}

   $products->with(['categories' => function($query) use ($filter_ids) {
    // Query for active categories
    $query->where('active', 1)->whereHas('filters', function ($query)  use ($filter_ids) {
        // Query for the selected filters from the request 
        $query->whereIn('id', $filter_ids);
    });
   }]);


$response = $products->limit(10)->get();

return response()->json([
    'status' => 'success',
    'response' => $response
], 200);

Lukas的回答让我做了更多的调试并最终解决了我的问题,尽管这不是if语句检查是否有搜索词/单词的位置

问题在于以下几行:

$products->limit(10)->get()

我需要存储从
get()检索到的结果方法,在我的示例中:

$response=$products->limit(10)->get()

我最终得到了以下工作代码:

// Create array from selected categories/filters
$filter_ids = explode(',', $request->get('cats'));

// Query for active products
$products = Product::where('active', '=', 1);
$products->with(['categories' => function($query) use ($filter_ids) {
    // Query for active categories
    $query->where('active', 1)->whereHas('filters', function ($query)  use ($filter_ids) {
        // Query for the selected filters from the request 
        $query->whereIn('id', $filter_ids);
    });
}]);

// Check for search term/word
if ($request->get('q')) {
    $q = $request->get('q') ? urldecode($request->get('q')) : null;
    $products->where('title', 'LIKE', "%{$q}%");
}

// Limit to 10 items, get results and store in '$response'
$response = products->limit(10)->get();

return response()->json([
    'status' => 'success',
    'response' => $response
], 200);

嗨,卢卡斯,谢谢你的老回答和我自己找到解决方案后的编辑:)