将Laravel查询构建器转换为雄辩的构建器 [英] Convert Laravel Query builder to Eloquent builder

查看:54
本文介绍了将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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆