将Laravel查询构建器转换为雄辩的构建器 [英] Convert Laravel Query builder to Eloquent builder
本文介绍了将Laravel查询构建器转换为雄辩的构建器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因为查询生成器的get()
函数在我需要A集合时返回一个数组,有没有办法将Laravel查询生成器转换为Eloquent Builder?
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?
$query_builder = DB::table('table1');
// different than
$eloquent_builder = Table1Model::select()
推荐答案
Laravel附带了collect
帮助器,可将数组转换为集合:
Laravel ships with a collect
helper to convert an array to a collection:
$collection = collect(DB::table('table1')->get());
有有关Github的提案,其中要有来自Laravel的下一个版本的回收集合实例查询构建器的get
方法.
There's a proposal on Github to have the next version of Laravel return collection instances from the query builder's get
method.
这篇关于将Laravel查询构建器转换为雄辩的构建器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文