We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
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:
<?php
$key = Post::all()->pluck('id');
You better do this:
<?php
$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.