在 Laravel 中,可以通过以下两种方式将数据库模型转换为集合(collection):

  1. 使用 get() 方法获取模型的集合:
$users = User::where('status', 1)->get();
$collection = $users->toCollection();
  1. 使用 collect() 函数将模型数组转换为集合:
$users = User::where('status', 1)->get()->toArray();
$collection = collect($users);
laravel 数据库模型转成collection

原文地址: https://www.cveoy.top/t/topic/ePG8 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录