
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 get last record from object collection in laravel
- 419 page expired error in Laravel
- How to display HTML tags In Laravel blade
- How to prevent host header attack in Laravel
- How to check column value of a record is null or not in laravel
- Target class [admin] does not exist.
- How to check duplicate entry in laravel
- Validation for multiple forms on same page in laravel
- Retain selected value of select box in Laravel
- How to get last month records in Laravel
- How to return error message from controller to view in laravel
- Send id with route Laravel
- SQLSTATE[HY000]: General error: 1215 Cannot add foreign key constraint
- How to create projects method with belongstomany relationship in user model
- How to check relationship is loaded or not in Laravel
- Laravel get single row by id
- Laravel append URI in route
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- Redirect to another view from controller in laravel
- Get posts belongs to a specific user in Laravel
- How to run a specific seeder class in laravel
- Non-static method App\Http\Helper::myFunction() should not be called statically
- Create project factory and seed data in laravel
- Display option of select as selected with blade directive Laravel
- Delete records with relationship in laravel