
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
- How to call Laravel route in jQuery
- Include External CSS and JS file in Laravel
- Permission denied error while creating storage link in Laravel
- Print query in laravel
- Laravel API response format
- How to fill a column automatically while creating records in Laravel
- Split an Eloquent Collection by half in Laravel
- Get current month records in laravel 7/8
- Use of undefined constant laravel
- Input file with max size validation in laravel
- Display message with session flash using bootstrap alert class in laravel
- How to add dynamic page title in Laravel view
- How to start websocket server in laravel
- Rendering HTML from database table to view in Laravel
- Remove array keys and values if it does not exist in other array in Laravel
- Laravel 5.4 save data to database
- Generate unique username in Laravel
- How to get path from current URL in Laravel
- How to display 1 day ago in comments in laravel view
- How to create event and listener in laravel ?
- Pass value from controller to model in laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- Get 30 days older records from table in laravel
- Get content from web URL in laravel
- Insert dummy data in users table Laravel