laravel 数据库模型转成collection
在 Laravel 中,可以通过以下两种方式将数据库模型转换为集合(collection):
- 使用
get()方法获取模型的集合:
$users = User::where('status', 1)->get();
$collection = $users->toCollection();
- 使用
collect()函数将模型数组转换为集合:
$users = User::where('status', 1)->get()->toArray();
$collection = collect($users);
原文地址: https://www.cveoy.top/t/topic/ePG8 著作权归作者所有。请勿转载和采集!