MySQL Laravel Fluent 查询 - 如何使用 Fluent 执行“SELECT AS”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14461623/
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
Laravel Fluent Queries - How do i perform a 'SELECT AS' using Fluent?
提问by adam Kearsley
I am quite new to Laravel and Fluent queries. I have a query to select all the rows from the hire table and display them in a random order.
我对 Laravel 和 Fluent 查询很陌生。我有一个查询来选择租用表中的所有行并以随机顺序显示它们。
DB::table('hire_bikes')->order_by(\DB::raw('RAND()'))->get();
I now want to be able to do is put
我现在想做的就是把
concat(SUBSTRING_INDEX(description, " ",25),"...") AS description
into the SELECT part of the query, so that i can select * from the table and a shortened description.
进入查询的 SELECT 部分,以便我可以从表中选择 * 和简短的描述。
I know this is possible by running a raw query, but i was hoping to be able to do this using Fluent or at least partial Fluent (like above).
我知道这可以通过运行原始查询来实现,但我希望能够使用 Fluent 或至少部分 Fluent(如上所述)来做到这一点。
Any help or ideas?
任何帮助或想法?
Thanks Adam.
谢谢亚当。
采纳答案by Alex Naspo
You can do this by by adding a DB::raw()
to a select array in your fluent query. I tested this locally and it works fine.
您可以通过将 a 添加DB::raw()
到流畅查询中的选择数组来完成此操作。我在本地进行了测试,效果很好。
DB::table('hire_bikes')
->select(
array(
'title',
'url',
'image',
DB::raw('concat(SUBSTRING_INDEX(description, " ",25),"...") AS description'),
'category'
)
)
->order_by(\DB::raw('RAND()'))
->get();
回答by Steve Bauman
You can actually use select AS
without using DB::raw()
. Just pass in an array into the select()
method like so:
您实际上可以使用 selectAS
而不使用DB::raw()
. 只需将数组传入select()
方法,如下所示:
$event = Events::select(['name AS title', 'description AS content'])->first();
// Or just pass multiple params
$event = Events::select('name AS title', 'description AS Content');
$event->title;
$event->content;
Tested just now.
刚刚测试过。
EDIT:
编辑:
Also I'd suggest against using a DB:raw()
query to perform a concat of your description field. If you're using an eloquent model you can use accessors & mutatatorsto perform this for you so if you ever need a limited description, you can simply output it in your view and not have to use the same query every time to get a limited description. For example:
此外,我建议不要使用DB:raw()
查询来执行描述字段的连接。如果您使用的是 eloquent 模型,您可以使用访问器和修改器来为您执行此操作,因此如果您需要有限的描述,您可以简单地将其输出到您的视图中,而不必每次都使用相同的查询来获得有限的描述。例如:
class Book extends Eloquent
{
public function getLimitedDescriptionAttribute()
{
return str_limit($this->attributes['description'], $limit = 100, $end = '...');
}
}
In your view:
在您看来:
@foreach($books as $book)
{{ $book->limited_description }}
@endforeach
Example Output (not accurate to limit):
示例输出(不准确到限制):
The description of this book is...
EDIT #2:
编辑#2:
I'd also advise against using the DB facade because it always utilizes your default connection. If you're querying a secondary connection, it won't take this into account unless you actively specify it using:
我还建议不要使用 DB 外观,因为它始终使用您的默认连接。如果您正在查询辅助连接,除非您使用以下方式主动指定它,否则它不会考虑这一点:
DB::connection('secondary')->table('hire_bikes')->select(['name as title'])->get();
EDIT #3:
编辑#3:
Just to note, if you use a select AS (name AS title
) and you wish to update your the model, you will still have to set the proper attribute name that coincides with your database column.
请注意,如果您使用 select AS ( name AS title
) 并且您希望更新您的模型,您仍然需要设置与您的数据库列一致的正确属性名称。
For example, this will cause an exception because the title
column does not exist in your database table:
例如,这将导致异常,因为该title
列在您的数据库表中不存在:
$event = Events::select('name AS title')->first();
$event->title = 'New name';
$event->save(); // Generates exception, 'title' column does not exist.
回答by malhal
select(array(DB::raw('latitude as lat'), DB::raw('longitude as lon')))