复杂搜索Laravel雄辩

时间:2019-11-23 04:36:38

标签: mysql laravel eloquent

晚安。我正在尝试建立一个搜索引擎,但是我对如何建立搜索一无所知。我有大约8个输入字段供用户输入,所有字段都可以为空/空白。在下面查看我要执行的操作:

在构建查询时我需要帮助,这样它才能真正起作用。当我运行此查询时,它将显示带有data_name1的数据。但是,如果我将标题从数据库的另一行放入,则该标题将不会显示任何内容。因此,当标题在多个不同行中都正确时,查询仅显示1个结果事件

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>ACE HTML Editor</title>
        <!-- Put editor language e.g.: Ace HTML Editor -->
        <style type="text/css" media="screen">
            #editor {
                position: absolute;
                top: 0;
                right: 0;
                bottom: 0;
                left: 0;
            }
        </style>
        <meta charset="UTF-8">
        <!-- Defines character set -->
        <link type="text/css" rel="stylesheet" href="../CSS/stylesheet.css">
        <!-- CSS Stylesheet -->
        <link type="image/x-icon" rel="shorcut icon" href="../Other/html5favicon.ico">
        <!-- Favicon -->
        <script type="text/javascript" src="../JavaScript/index.js"></script>
        <!-- JavaScript Index -->
    </head>
    <body>
        <div id="editnav">
            <input type="button" id="downloadbtn" onclick="downloadHTML()" value="Download">
            <input type="button" id="openbtn" onclick="openCode()" value="Open">
            <input type="button" id="homebtn2" onclick="window.location.href = 'index.html';" value="Home">
        </div>
        <input type="button" id="togglebtn2" onclick="toggleVisibility2()" value="Toggle">
        <div id="editor">&lt;!DOCTYPE html>
&lt;html>
    &lt;head lang="">
        &lt;meta charset="">
        &lt;link type="text/css" rel="stylesheet" href="">
        &lt;!-- CSS Stylesheet -->
        &lt;link type="image/x-icon" rel="shortcut icon" href="">
        &lt;!-- Favicon -->
        &lt;title>&lt;/title>
    &lt;/head>
    &lt;body>
        &lt;p>Ace HTML Editor&lt;/p>
    &lt;/body>
&lt;/html></div>
        <!-- In this div, put filler text -->
        <!-- use &lt; for < -->
        <script src="../Other/Ace/ace-builds-master/src/ace.js" type="text/javascript" charset="utf-8"></script>

    </body>
</html>

更新。但是使用下面的代码,它仅检查一行。如果条件匹配,我希望它从不同的行中带回数据

$data = DB::table('inventory')
    ->where('data_name1', 'like', $request->data_name1.'%')
    ->orWhere('data_name2', 'like', $request->data_name2.'%')
    ->orWhere('title', 'like', $request->title.'%')
    ->orWhere('caption', '=', $request->caption)
    ->whereIn('part', [$request->front, $request->back, $request->side, $request->center])
    ->orWhere('ppd', '=', $request->ppd)
    ->orWhere('user', '=', $request->user)
    ->whereBetween('year', [$request->startyear, $request->endyear])
    ->get();

1 个答案:

答案 0 :(得分:1)

您可以使用when和很多orWhere ...方法。这对您有用吗?

$data = DB::table('inventory')
->when($request->has('data_name1'), function ($query) use ($request) {
    $query->orWhere('data_name1', 'LIKE', $request->data_name1.'%');
})
->when($request->has('data_name2'), function ($query) use ($request) {
    $query->orWhere('data_name2', 'LIKE', $request->data_name2.'%');
})
->when($request->has('title'), function ($query) use ($request) {
    $query->orWhere('title', 'LIKE', $request->title.'%');
})
->when($request->has('caption'), function ($query) use ($request) {
    $query->orWhere('caption', 'LIKE', $request->caption.'%');
})
->when($request->has('part'), function ($query) use ($request) {
    $query->orWhere('part', $request->part);
})
// Alternatively
// ->when($request->has(['front', 'back', 'side', 'center']), function ($query) use ($request) {
//     $query->orWhereIn('part', [$request->front, $request->back, $request->side, $request->center]);
// })
->when($request->has('ppd'), function ($query) use ($request) {
    $query->orWhere('ppd', $request->ppd);
})
->when($request->has('user'), function ($query) use ($request) {
    $query->orWhere('user', $request->user);
})
->when($request->has(['startyear', 'endyear']), function ($query) use ($request) {
    $query->orWhereBetween('year', [$request->startyear, $request->endyear]);
})
->get();