将Laravel查询构建器转换为Eloquent构建器

4

因为 Laravel Query Builder 的 get() 函数返回的是一个数组,而我需要一个集合,是否有一种方法可以将 Laravel Query Builder 转换为 Eloquent Builder?

$query_builder = DB::table('table1');

// different than
$eloquent_builder = Table1Model::select()

1
你可以从数组创建一个集合:Collection::make($query_builder)。这里是一个使用示例:http://stackoverflow.com/questions/33449387/laravel-creating-different-views-from-query/33449507#33449507 - Iamzozo
1个回答

9

Laravel带有一个collect助手函数,用于将数组转换为集合:

$collection = collect(DB::table('table1')->get());

有一份在Github上的提案,希望Laravel的下一个版本可以从查询构建器的get方法返回集合实例。


2
在 Laravel 5.4 和 5.5 中,查询构建器(DB::table)返回 collection - asmmahmud

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,