Get Array of IDs from Eloquent Collection
Get Array of IDs from Eloquent Collection
With this article, we’ll look at some examples of how to address the "Get Array of IDs from Eloquent Collection" problem.
-
Get array of IDs from Eloquent relationships using pluck function
--PATH app\Http\Controllers\<YourController>.php$post = Post::where('id',1)->first(); $commentIDs = $post->comments->pluck('id'); //Data in object collection print_r($commentIDs); //Data in array print_r($commentIDs->toArray());
Sometimes you have to get the Array of IDs from eloquent collection for some records. Imagine, that you have hasMany() relationship – one Post has many Comments. And then you want to get IDs of comments by a certain Post. You can get the IDs of comments of the related post using pluck function.
Output
Data in Collection Object
Illuminate\Support\Collection Object ( [items:protected] => Array ( [0] => 3 [1] => 4 [2] => 5 ) )
Data in Array
Array ( [0] => 3 [1] => 4 [2] => 5 )
-
Get array of IDs from Eloquent relationships using modelKeys function
--PATH app\Http\Controllers\<YourController>.php$post = Post::where('id',1)->first(); $commentIDs = $post->comments->modelKeys(); print_r($commentIDs); $comments = Comment::whereIn('id', $commentIDs)->get();
You can use modelKeys method on eloquent relationship method comments (hasMany) to get the IDs of the comments table records which are related to this particular post. You can also get all the comments using these IDs with whereIn method in laravel. The value of $commentIDs should be in array format.
Output
Array ( [0] => 3 [1] => 4 [2] => 5 )
If you like what you are reading, please consider buying us a coffee ( or 2 ) as a token of appreciation.
Don't forget to share this article! Help us spread the word by clicking the share button below.
We appreciate your support and are committed to providing you valuable and informative content.
We are thankful for your never ending support.
Random Code Snippet Queries: Laravel
- Pass value from controller to model in laravel
- Create record with unique slug in laravel
- How to display pivot table column value in laravel
- How to use or operator in laravel
- How to create event and listener in laravel ?
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Get count of filter data, while return a small set of records
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Calculate age from date of birth in Laravel
- Sample configuration files to create laravel project with docker using wsl (window subsystem linux)
- Credit card validation in laravel
- FirstOrCreate() Not Inserting Model
- Route [password.request] not defined
- How to send ID to another page in Laravel
- Convert multidimensional array to single array in Laravel
- Use withCount() to get total number of records with relationship
- Cast Array to an Object in Controller and then pass to view in laravel
- Laravel form request validation
- How to get session in blade Laravel ?
- Insert Comma Separated Values in laravel
- How to pass data to multiple partial view files in laravel
- Composer\Exception\NoSslException
- How to call Laravel route in jQuery
- How to add columns in existing table using migration in laravel
- Send post data from controller to view