PHP Laravel 框架中 whereRaw 是什么意思

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42927037/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:43:49  来源:igfitidea点击:

What is meant by whereRaw in PHP Laravel framework

phplaravel

提问by kavi krish

I'm unsure what whereRawis in PHP Laravel framework. Could you provide good and easily understandable example, please?

我不确定whereRawPHP Laravel 框架中有什么。你能提供一个简单易懂的例子吗?

回答by Parantap Parashar

WhereRaw()is a function of Laravel query builder which puts your input as it is in the SQL query's where clause.

WhereRaw()是 Laravel 查询构建器的一个函数,它将您的输入按原样放在 SQL 查询的 where 子句中。

Think of it as the where()function whose input argument will not be processed before inserting into queries.

将其视为where()在插入查询之前不会处理其输入参数的函数。

See the example below:

请参阅下面的示例:

$Query = DB::table('some_table')->where('YEAR(date)', 'YEAR(CURRENT_DATE)');

In this Laravel will resolve your arguments to build a query. Which will result in the following query because your input will be treated as some fieldand its its value:

在此 Laravel 将解析您的参数以构建查询。这将导致以下查询,因为您的输入将被视为 somefield及其它的value

SELECT * FROM `some_table` WHERE `YEAR(date)` = `YEAR(CURRENT_DATE)`

Which is not desired.

这是不希望的。

And now if you use whereRawlike:

现在如果你使用whereRaw像:

$Query = DB::table('some_table')->whereRaw('YEAR(date) = YEAR(CURRENT_DATE)');

Now Laravel put this where clause as it is in your query, like below:

现在 Laravel 把这个 where 子句放在你的查询中,如下所示:

SELECT * FROM `some_table` WHERE YEAR(date) = YEAR(CURRENT_DATE)

Hope it helped (:

希望它有所帮助(:

回答by Mayank Pandeyz

WhereRaw:Sometimes you may need to use a raw expression in a query. These expressions will be injected into the query as strings.

WhereRaw:有时您可能需要在查询中使用原始表达式。这些表达式将作为字符串注入到查询中。

If you are unable to generate the query you need via the fluent interface, feel free to use whereRaw()

如果您无法通过流畅的界面生成您需要的查询,请随意使用 whereRaw()

Ex:

前任:

$users = User::whereRaw('age > ? and votes = 100', array(25))->get();

which is equals to:

这等于:

"SELECT * FROM users WHERE age > 25 AND votes = 100";

Reference

参考

回答by Dhanushka Udayanga

In laraval we use query builder. But Sometime you need to execute raw sql query.

在laraval 中,我们使用查询构建器。但有时您需要执行原始 sql 查询。

So you can inject it to whereRawas this example :

所以你可以whereRaw像这个例子一样注入它:

whereRAW('YEAR(event_datetime) =?', [$year])