【问题标题】:Laravel and Eloquent - applying different filtersLaravel 和 Eloquent - 应用不同的过滤器
【发布时间】:2020-02-27 06:38:38
【问题描述】:

我感觉卡住了。 :(

我希望能够执行不同的 SQL 查询,具体取决于我的表单中选择的过滤器:

//My initial query without any filters is this:
$dbQuery="SELECT * FROM \"interactions\" WHERE \"user_id\" = ".Auth::user()->getAttribute('id');
//Then depending on the selected filters the query may be any combination of the following:
if (request('contact_id')) $dbQuery.=" AND \"contact_id\" = ".request('contact_id');
if (request('product_id')) $dbQuery.=" AND \"product_id\" = ".request('product_id');
if (request('type')) $dbQuery.=" AND \"type\" LIKE \"%".request('type')."%\"";
if (request('description')) $dbQuery.=" AND \"description\" LIKE \"%".request('description')."%\"";
if (request('date')) $dbQuery.=" AND \"date\" >= ".request('date');

我有一个名为“Interaction”的类,它扩展了 Eloquent 模型,我需要能够执行上述查询或通过它表示相同的逻辑。

任何关于我如何实现这一目标的想法将不胜感激!

编辑: 感谢 Brice(我今天的个人英雄),这就是我的诀窍:

$query = Interaction::where('user_id', Auth::id());
$contact_id = request('contact_id');
$product_id = request('product_id');
$type = request('type');
$description = request('description');
$date = request('date');
if ($contact_id) $query->where('contact_id', $contact_id);
if ($product_id) $query->where('product_id', $product_id);
if ($type) $query->where('type', 'like', "%".$type."%");
if ($description) $query->where('description', 'like', "%".$description."%");
if ($date) $query->where('date', '>=', $date);
$interactions = $query->get();
return view('interactions.index',compact('interactions'));

【问题讨论】:

  • 您在实现这一目标时遇到了哪些具体问题?你试过什么?附带说明一下,目前您的查询使您容易受到注入问题的影响 - 您应该在查询中使用绑定,而不是将数据值直接放入查询中。
  • 嗨,zbee!我对 Eloquent 很陌生,对我来说有点模糊。我浏览了很多文章和文档,但似乎找不到合适的函数来表示上述逻辑。任何方向将不胜感激!我知道注射的问题,但这是我现在最不关心的问题。

标签: php sql laravel eloquent


【解决方案1】:

我建议为此使用 eloquent 查询生成器。

例如:

$query = Interaction::where('user_id', Auth::id());

$contact_id = request('contact_id');
$product_id = request('product_id');
$type = request('type');
$description = request('description');
$date = request('date');

if ($contact_id) {
    $query->where('contact_id', $contact_id);
}

if ($product_id) {
    $query->where('product_id', $product_id);
}

if ($type) {
    $query->where('type', 'like', "%$type%");
}

if ($description) {
    $query->where('type', 'like', "%$description%");
}

if ($date) {
    $query->where('date', '>=', \Carbon\Carbon::parse($date));
}

$results = $query->get();

如果您有很多结果,您可能希望使用分页,而不是如上所示同时获取所有结果。

【讨论】:

  • 谢谢你,布莱斯!分页将是下一步...... :) 还有一个问题:这样,当用户数据通过 Eloquent 时,我是否需要担心注入问题?
  • @Kalo 不,使用查询构建器是安全的,因为 Laravel 在后台使用参数绑定。请参阅:laravel.com/docs/5.8/queries#introduction“Laravel 查询构建器使用 PDO 参数绑定来保护您的应用程序免受 SQL 注入攻击。无需清理作为绑定传递的字符串。”
  • 非常感谢您的宝贵时间,布莱斯!你让我省了很多麻烦。我对您的代码进行了一些调整,现在一切正常。我已经接受了您的回答并使用工作代码编辑了我的问题。 :)
  • 乐于助人! :)
【解决方案2】:

你可以使用->when()方法docs

$results = Interaction::where('user_id', Auth::id())
    ->when($contact_id, function ($query) use ($contact_id) {
        $query->where('contact_id', $contact_id);
    })
    ->when($product_id, function ($query) use ($product_id) {
        $query->where('product_id', $product_id);
    })
    ->when($type, function ($query) use ($type) {
        $query->where('type', 'like', "%$type%");
    })
    ->when($description, function ($query) use ($description) {
        $query->where('type', 'like', "%$description%");
    })
    ->when($date, function ($query) use ($date) {
        $query->where('date', '>=', \Carbon\Carbon::parse($date));
    })->get();;

【讨论】:

    猜你喜欢
    • 2021-08-14
    • 2015-04-21
    • 1970-01-01
    • 2014-12-27
    • 2023-03-08
    • 2022-07-11
    • 2021-11-18
    • 1970-01-01
    • 2012-04-05
    相关资源
    最近更新 更多