将 Laravel 查询构建器转换为 Eloquent 构建器
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33630472/
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
Convert Laravel Query builder to Eloquent builder
提问by Omid
Because get()
function of Query builder returns an array while I need A collection, is there a way to convert Laravel Query Builder to Eloquent Builder?
因为get()
查询构建器的函数在我需要一个集合时返回一个数组,有没有办法将 Laravel 查询构建器转换为 Eloquent Builder?
$query_builder = DB::table('table1');
// different than
$eloquent_builder = Table1Model::select()
回答by Joseph Silber
Laravel ships with a collect
helper to convert an array to a collection:
Laravel 附带了一个collect
将数组转换为集合的助手:
$collection = collect(DB::table('table1')->get());
There's a proposal on Githubto have the next version of Laravel return collection instances from the query builder's get
method.
有在Github上的提案有Laravel返回集合实例从查询生成器的下一个版本的get
方法。