How to get IDs from a collection in Laravel
Categorized as Laravel
You can always retrieve IDs from Eloquent Collection in Laravel with the built-in method modelKeys
:
// 🤔 Could be this statement
$ids = Category::all()->pluck('id');
// ✅ Could also be this statement
$ids = Category::all()->modelKeys();
dd($ids);
As the official documentation says,
theÂ
modelKeys
 method returns the primary keys for all models in the collection
$users->modelKeys();
// [1, 2, 3, 4, 5]