Laravel 集合所在索引
Laravel Collection Where Index
我下面有这个合集
[
{
"id":"6ad91b63-d964-4155-b16a-47fc246b6aa3",
"content":"",
"to":[
"6ea94472-7db6-4d66-a485-30f99d46d039",
"37c61b0b-2b75-4652-9ee9-b8d54fde3fe9",
"335e9ee7-02ff-494d-89e9-b69b76d0504b",
"7f67f3aa-7fdf-461c-aaf9-90293067363f",
"a30477c8-256c-4ba8-aa69-22d7190516dd"
],
"type":"berita",
"created_by":"363b75f2-8f01-422d-a3cb-ea501d9b5edd",
"created_at":"2022-01-12T18:22:51.000000Z",
"updated_at":"2022-01-12T18:23:47.000000Z"
}
]
我想按“to”列查找,其中“to”是我定义的值。我试图使用 where collection 但仍然一无所获。这是我的 eloquent 电话
$feed = Feed::latest()
->get()
->where('to','a30477c8-256c-4ba8-aa69-22d7190516dd');
您可以直接使用 filter
(where
使用):
$collection->filter(fn ($item) => in_array('a30477c8-256c-4ba8-aa69-22d7190516dd', $item->to))
Laravel 8.x Docs - Collections - Available Methods - filter
PHP.net Manual - Function Reference - Arrays - Array Functions - in_array
我下面有这个合集
[
{
"id":"6ad91b63-d964-4155-b16a-47fc246b6aa3",
"content":"",
"to":[
"6ea94472-7db6-4d66-a485-30f99d46d039",
"37c61b0b-2b75-4652-9ee9-b8d54fde3fe9",
"335e9ee7-02ff-494d-89e9-b69b76d0504b",
"7f67f3aa-7fdf-461c-aaf9-90293067363f",
"a30477c8-256c-4ba8-aa69-22d7190516dd"
],
"type":"berita",
"created_by":"363b75f2-8f01-422d-a3cb-ea501d9b5edd",
"created_at":"2022-01-12T18:22:51.000000Z",
"updated_at":"2022-01-12T18:23:47.000000Z"
}
]
我想按“to”列查找,其中“to”是我定义的值。我试图使用 where collection 但仍然一无所获。这是我的 eloquent 电话
$feed = Feed::latest()
->get()
->where('to','a30477c8-256c-4ba8-aa69-22d7190516dd');
您可以直接使用 filter
(where
使用):
$collection->filter(fn ($item) => in_array('a30477c8-256c-4ba8-aa69-22d7190516dd', $item->to))
Laravel 8.x Docs - Collections - Available Methods - filter
PHP.net Manual - Function Reference - Arrays - Array Functions - in_array