简体   繁体   English

生成器中的laravel 5.3原始SQL语句

[英]laravel 5.3 raw SQL statement in Builder

I have applied a laravel builder to filter search input by user in a form, but it isn't working optimal. 我已经应用了一个laravel构建器来过滤用户以某种形式输入的搜索,但是它并不是最佳的。 This is my scenario: 这是我的情况:

my form looks like this: 我的表格如下所示: 表格搜索功能

in the first input is custom query search where the following builder is made. 在第一个输入中是定制查询搜索,在其中创建以下构建器。

return $builder->where('city', 'LIKE', '%' . $value . '%')
                   ->orWhere('first_name', 'LIKE', '%' . $value . '%')
                   ->orWhere('middle_name', 'LIKE', '%' . $value . '%')
                   ->orWhere('last_name', 'LIKE', '%' . $value . '%');

the 3 dropdown menu's are filtered by the following code: 下面的代码过滤了3个下拉菜单:

return $builder->where('location', $value); // <-- for "selecteer locatie"
return $builder->where('level', $value); // <-- for "selecteer richting"
return $builder->where('graduation', $value); // <-- for "selecteer diplomajaar"

$value is the input of the user. $ value是用户的输入。

Now whenever I filter by the 3 dropdown options, it works as magic. 现在,只要我按3个下拉选项进行过滤,它就会像魔术一样起作用。 Whenever i try to combine it with the Query of the first filter, it shows more results than i actually want. 每当我尝试将其与第一个过滤器的查询组合时,它就会显示比实际所需更多的结果。

So the first query is giving an OR-statement and that actually is the bug. 因此,第一个查询给出的是OR语句,这实际上是错误。 I want to place a RAW SQL statement in the builder where I can run 我想在可以运行的构建器中放置RAW SQL语句

 "WHERE `city` LIKE %" . $value . "% 
 OR `first_name` LIKE %" . $value . " 
 OR `middle_name` LIKE %" . $value . " 
 OR `last_name` LIKE %" . $value . " 

in between these symbols ( 'statement' ) 在这些符号之间( 'statement' )

Is there a way where i can run the builder separately or a way how I can input a raw SQL statement in the laravel 5.3 builder? 有没有一种方法可以单独运行构建器,也可以在laravel 5.3构建器中输入原始SQL语句?

If you need more information, please ask. 如果您需要更多信息,请询问。

EDIT: My question is not clear enough! 编辑:我的问题还不够清楚! see description below. 请参阅下面的说明。

I am using the Builder class to make my SQL filter in my form. 我正在使用Builder类在表单中创建SQL过滤器。 Whenever I run the advanced search and dump my Builder, I get a Builder object with the following array in the +where clause: 每当我运行高级搜索并转储我的Builder时,我都会在+ where子句中获得带有以下数组的Builder对象:

+wheres: array:7 [▼
0 => array:5 [▼
  "type" => "Basic"
  "column" => "city"
  "operator" => "LIKE"
  "value" => "%hof%"
  "boolean" => "and"
]
1 => array:5 [▼
  "type" => "Basic"
  "column" => "first_name"
  "operator" => "LIKE"
  "value" => "%hof%"
  "boolean" => "or"
]
2 => array:5 [▼
  "type" => "Basic"
  "column" => "middle_name"
  "operator" => "LIKE"
  "value" => "%hof%"
  "boolean" => "or"
]
3 => array:5 [▼
  "type" => "Basic"
  "column" => "last_name"
  "operator" => "LIKE"
  "value" => "%hof%"
  "boolean" => "or"
]
4 => array:5 [▼
  "type" => "Basic"
  "column" => "location"
  "operator" => "="
  "value" => "Franeker"
  "boolean" => "and"
]
5 => array:5 [▼
  "type" => "Basic"
  "column" => "level"
  "operator" => "="
  "value" => "MAVO"
  "boolean" => "and"
]
6 => array:5 [▼
  "type" => "Basic"
  "column" => "graduation"
  "operator" => "="
  "value" => "1992"
  "boolean" => "and"
]
]

Because I am using an OR-operator for comparing with city , first_name , middle_name or last_name , I should be getting 4 where clauses instead of 7 . 因为我使用OR运算符与cityfirst_namemiddle_namelast_name进行比较, middle_name我应该得到4个where子句,而不是7个 This is caused because the Builder sees the OR-statement called in the first input as an AND-statement. 这是因为生成器将第一个输入中调用的OR语句视为AND语句。

I solved it by replacing this: 我通过替换它解决了这个问题:

return $builder->where('city', 'LIKE', '%' . $value . '%')
                   ->orWhere('first_name', 'LIKE', '%' . $value . '%')
                   ->orWhere('middle_name', 'LIKE', '%' . $value . '%')
                   ->orWhere('last_name', 'LIKE', '%' . $value . '%');

into this: 到这个:

return $builder->where( function($query) use ($value){

    $query->where('city', 'LIKE', '%' . $value . '%')
          ->orWhere('first_name', 'LIKE', '%' . $value . '%')
          ->orWhere('middle_name', 'LIKE', '%' . $value . '%')
          ->orWhere('last_name', 'LIKE', '%' . $value . '%');
});

EDIT: the following +where clauses is dumped when die and dumping the Builder class 编辑:死时并转储Builder类时转储了以下+ where子句

+wheres: array:4 [▼
 0 => array:3 [▼
  "type" => "Nested"
  "query" => Builder {#216 ▶}
  "boolean" => "and"
 ]
 1 => array:5 [▼
  "type" => "Basic"
  "column" => "location"
  "operator" => "="
  "value" => "Franeker"
  "boolean" => "and"
 ]
 2 => array:5 [▼
  "type" => "Basic"
  "column" => "level"
  "operator" => "="
  "value" => "MAVO"
  "boolean" => "and"
 ]
 3 => array:5 [▼
  "type" => "Basic"
  "column" => "graduation"
  "operator" => "="
  "value" => "1992"
  "boolean" => "and"
 ]
]

In the nested Builder class at array index 0 is the OR-statement inserted. 在数组索引为0的嵌套Builder类中,插入了OR语句。

Your answer should correctly group the wheres on their own. 您的答案应该正确地将地点分组。

For "how I can input a raw SQL statement in the Laravel 5.3 builder?" 对于“如何在Laravel 5.3构建器中输入原始SQL语句?”

DB::statement($rawStatement)

Make sure you bind the values as the second parameter to statement , you can also use 确保将值作为第二个参数绑定到statement ,也可以使用

DB::statement(DB::raw($rawStatement))

There's also 还有

 $query->whereRaw("city LIKE %{$value}%")

Which can be useful for entering some queries but would not solve your nested query issue. 这对于输入某些查询很有用,但不能解决嵌套查询问题。

If you wanted to make all of the wheres raw you would have to do something along the lines of : 如果您想将所有原始资料制作成原始内容,则必须遵循以下步骤:

$query->whereRaw($mainQuery)
->setBindings([$city, $first_name, $middle_name,  $last_name])

Where main query looks like: 主查询如下所示:

"(city LIKE ? OR first_name LIKE ? OR middle_name LIKE ? OR last_name LIKE ?)"

Then you can append your raw filtered query to that string 然后,您可以将原始筛选查询附加到该字符串

"AND location = ?"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM