Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/270.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php Laravel和雄辩-应用不同的过滤器_Php_Sql_Laravel_Eloquent - Fatal编程技术网

Php Laravel和雄辩-应用不同的过滤器

Php Laravel和雄辩-应用不同的过滤器,php,sql,laravel,eloquent,Php,Sql,Laravel,Eloquent,我觉得卡住了( 我希望能够执行不同的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

我觉得卡住了(

我希望能够执行不同的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”的类,它扩展了雄辩的模型,我需要能够执行上述查询或通过它表示相同的逻辑

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

编辑: 多亏了布里斯(我今天的个人英雄),以下是我成功的秘诀:

$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'));

我建议为此使用雄辩的查询生成器

例如:

$query=Interaction::where('user_id',Auth::id());
$contact_id=请求(“contact_id”);
$product_id=请求(“product_id”);
$type=请求('type');
$description=请求('description');
$date=请求(“日期”);
如果($contact\u id){
$query->where('contact\u id',$contact\u id);
}
如果($product\U id){
$query->where('product\u id',$product\u id);
}
如果($type){
$query->where('type','like',“%$type%”);
}
如果($说明){
$query->where('type','like',“%$description%”);
}
如果($日期){
$query->where('date','>=',\Carbon\Carbon::parse($date));
}
$results=$query->get();
如果有很多结果,您可能希望使用分页,而不是如上所示同时获取所有结果。

您可以使用
->when()
方法


在实现这一点时,您有什么具体的困难?您尝试过什么?另一方面,目前您的查询容易出现注入问题-您应该在查询中使用绑定,而不是直接将数据值放入查询中。嗨,zbee!我对雄辩很陌生,对我来说有点模糊。我经历过有很多文章和文档,但似乎找不到正确的函数来表示上述逻辑。任何方向都将受到欢迎!我知道注入的问题,但这是我现在最不关心的问题。谢谢你,Brice!分页将是下一步…:)还有一个问题:这样,当用户数据通过Elount时,我是否需要担心注入问题?@Kalo Nope,使用查询生成器时您是安全的,因为在幕后Laravel使用参数绑定。请参阅:“Laravel查询生成器使用PDO参数绑定来保护您的应用程序免受SQL注入攻击。不需要清除作为绑定传递的字符串。”非常感谢您的时间,Brice!你帮我省去了很多麻烦。我对你的代码做了一些调整,现在一切都很好。我已接受您的回答,并使用工作代码编辑了我的问题。:)很乐意帮忙!:)
$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();;