#development #eloquent #laravel #php
One of the lesser-known but highly useful functions in Laravel is modelKeys
.
The modelKeys
method returns the primary keys for all models in the collection?
So, instead of doing this:
1$key = Post::all()->pluck('id');
You better do this:
1$keys = Post::all()->modelKeys();
The $keys
variable will contain an array with the primary key column values. The key types are typically 'int' for
integer primary keys and 'string' for string-based primary keys, but they can vary depending on your migration setup.
If this post was enjoyable or useful for you, please share it! If you have comments, questions, or feedback, you can email my personal email. To get new posts, subscribe use the RSS feed.