Lumen/Laravel - 从对象数组中的对象中提取特定 属性 的方法?
Lumen/Laravel - Method to extract specific property from objects in an array of objects?
我有以下数据:
[{
"id": 1,
"name": "test1",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 2,
"name": "test12",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 3,
"name": "test123",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 4,
"name": "test1234",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 5,
"name": "test12345",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 6,
"name": "test123456",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 7,
"name": "test1234567",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 8,
"name": "test12345678",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 9,
"name": "test123456789",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 10,
"name": "test1234567890",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 11,
"name": "test12345678901",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 12,
"name": "test123456789012",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}]
此数据是通过 eloquent 模型检索的,如下所示:
$ad_groups = Ad_user::find($request->decodedToken->user_id)->ad_groups()->get();
我不知道 eloquent 模型的任何 "inherited" 功能是否仍然存在于 $ad_groups
中,这就是为什么我在这里为您发布它以便您知道我们必须工作的原因用 ^^
我基本上想要完成的是提取 ID。我当然可以只写一个 foreach 循环。但是我是 Lumen/Laravel 的新手,我想知道在这种情况下是否还有一些很棒的方法,比如 ...>getMeWhatIWant()
可用:D
您可以在 Laravel 集合 (official docs here) 上使用 pluck
方法。
您只需将要检索的列名作为第一个参数传递:
$ids = $ad_groups->pluck('id');
dd($ids);
我有以下数据:
[{
"id": 1,
"name": "test1",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 2,
"name": "test12",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 3,
"name": "test123",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 4,
"name": "test1234",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 5,
"name": "test12345",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 6,
"name": "test123456",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 7,
"name": "test1234567",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 8,
"name": "test12345678",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 9,
"name": "test123456789",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 10,
"name": "test1234567890",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 11,
"name": "test12345678901",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}, {
"id": 12,
"name": "test123456789012",
"created_at": "2020-01-30 15:55:44",
"updated_at": "2020-01-30 15:55:44"
}]
此数据是通过 eloquent 模型检索的,如下所示:
$ad_groups = Ad_user::find($request->decodedToken->user_id)->ad_groups()->get();
我不知道 eloquent 模型的任何 "inherited" 功能是否仍然存在于 $ad_groups
中,这就是为什么我在这里为您发布它以便您知道我们必须工作的原因用 ^^
我基本上想要完成的是提取 ID。我当然可以只写一个 foreach 循环。但是我是 Lumen/Laravel 的新手,我想知道在这种情况下是否还有一些很棒的方法,比如 ...>getMeWhatIWant()
可用:D
您可以在 Laravel 集合 (official docs here) 上使用 pluck
方法。
您只需将要检索的列名作为第一个参数传递:
$ids = $ad_groups->pluck('id');
dd($ids);