Laravel 包含模型到数组的集合
Laravel collection containing model to array
我有一个 Laravel 集合,其中包含多个用户模型。
Collection([
User,
User,
])
模型包含例如用户名、名字、姓氏、电子邮件、出生日期。我只需要用户名、电子邮件、日期并将其放在这样的数组中。
array(
array('jonhdoe', 'johndoe@example.com', '1-1-1970')
)
以便我可以使用
访问它
$username = $array[0][0]
$email = $array[0][1]
而不是
$username = $array[0]['username']
$email = $array[0]['email']
我正在查看每个 Laravel 个助手或映射,但不知何故我无法让它工作。
only()
将 return 仅指定列的数组。 toArray()
会将集合转换为数组:
$collection->map(function($i) {
return array_values($i->only('username', 'email', 'date'));
})->toArray();
使用toArray()-
$collection_in_arrays = $collection->toArray();
The toArray
method converts the collection into a plain PHP array
. If the collection's
values are Eloquent models, the models will also be converted to arrays
.
我有一个 Laravel 集合,其中包含多个用户模型。
Collection([
User,
User,
])
模型包含例如用户名、名字、姓氏、电子邮件、出生日期。我只需要用户名、电子邮件、日期并将其放在这样的数组中。
array(
array('jonhdoe', 'johndoe@example.com', '1-1-1970')
)
以便我可以使用
访问它$username = $array[0][0]
$email = $array[0][1]
而不是
$username = $array[0]['username']
$email = $array[0]['email']
我正在查看每个 Laravel 个助手或映射,但不知何故我无法让它工作。
only()
将 return 仅指定列的数组。 toArray()
会将集合转换为数组:
$collection->map(function($i) {
return array_values($i->only('username', 'email', 'date'));
})->toArray();
使用toArray()-
$collection_in_arrays = $collection->toArray();
The
toArray
method converts the collection into a plainPHP array
. If thecollection's
values are Eloquent models, the models will also be converted toarrays
.