从收藏品中获取钥匙

kjd*_*n84 3 php collections laravel

如何从收藏品中获取钥匙?

$posts= Post::all();

示例(不起作用):

$key = $posts->where('id', $id)->getKey();

Ale*_*nin 6

all()返回一个没有键的集合。如果您谈论的是 0、1、2 等整数键,请使用以下search()方法:

$key = $posts->search(function($i) use($id) {
    return $i->id === $id;
});
Run Code Online (Sandbox Code Playgroud)