laravel 将 MySQL 函数传递给 Eloquent ORM 查询
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14266298/
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
Passing MySQL functions to Eloquent ORM query
提问by NightMICU
I am trying to execute the following query in Eloquent ORM and cannot seem to execute the MySQL function -
我试图在 Eloquent ORM 中执行以下查询,但似乎无法执行 MySQL 函数 -
$post = Post::where('slug', '=', $slug)->where('YEAR(created_at)', '=', $year)->first();
The exception I am getting is as follows - Message:
我得到的异常如下 - 消息:
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'YEAR(created_at)' in 'where clause'
SQL: SELECT * FROM `posts` WHERE `slug` = ? AND `YEAR(created_at)` = ? LIMIT 1
Bindings: array (
0 => 'placeholder',
1 => 2013,
)
So, basically, it is encapsulating the YEAR()
MySQL function as a column. Is there any way to do this without using a raw query?
所以,基本上,它是将YEAR()
MySQL 函数封装为一个列。有没有办法在不使用原始查询的情况下做到这一点?
采纳答案by Mike Brant
You probably don't want to use the YEAR()
function in your WHERE clause anyway. This would prevent you from using any index on the created_at
column. I would suggest you use LIKE
instead:
YEAR()
无论如何,您可能不想在 WHERE 子句中使用该函数。这将阻止您在created_at
列上使用任何索引。我建议你LIKE
改用:
$post = Post::where('slug', '=', $slug)->where('created_at', 'LIKE', $year . '%')->first();
You can also just use raw SQL queries as well (using query()
method) if you had need to utilize unsupported MySQL functions in your queries.
query()
如果您需要在查询中使用不受支持的 MySQL 函数,您也可以只使用原始 SQL 查询(使用方法)。
回答by ULazdins
To prevent Eloquent ORM from wrapping first variable with apostrophes, you can use DB:raw
function like:
为了防止 Eloquent ORM 用撇号包装第一个变量,你可以使用如下DB:raw
函数:
$post = Post::where('slug', '=', $slug)
->where(DB::raw('YEAR(created_at)'), '=', $year)
->first();
And you'll get query:
你会得到查询:
SELECT * FROM `posts` WHERE `slug` = ? AND YEAR(created_at) = ? LIMIT 1
回答by vipmaa
You have two ways to do this and its depend on your laravel version
您有两种方法可以做到这一点,这取决于您的 Laravel 版本
First use Rawmethod to pass function as below example
首先使用Raw方法传递函数,如下例所示
$post = Post::where('slug', $slug)
->where(DB::raw('YEAR(created_at)'), $year)
->first();
?Also you can use it in (select,group,order) Methods, for more information about Raw Laravel Docsits start from v 4.2
?您也可以在 (select,group,order) 方法中使用它,有关从 v 4.2 开始的有关 Raw Laravel Docs 的更多信息
Second use whereYearMethod
第二种使用whereYear方法
$post = Post::where('slug', $slug)
->whereYear('created_at', $year)
->first();
This method start from V 5.3 for more information Read Where sectionyou will found all methods for dates (whereDate / whereMonth / whereDay / whereYear)
此方法从 V 5.3 开始以获取更多信息,请阅读 Where 部分,您将找到所有日期方法(whereDate / whereMonth / whereDay / whereYear)
回答by Alex B. Santos
I use Laravel 5.3
我使用 Laravel 5.3
$post = Post::where('slug', '=', $slug)->whereYear('created_at', '=', $year)->first();
This guy helped me >> https://stackoverflow.com/a/32843415/7752468