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
- Use of undefined constant laravel
- Retain selected value of select box in Laravel
- JQuery each loop on json response after ajax in laravel
- How to remove package from laravel
- Input file with max size validation in laravel
- Laravel route redirect not working
- Laravel get all records with pagination
- How to display validation error in laravel
- Send OTP using textlocal api in laravel
- How to get data from two tables in laravel
- Laravel 10 starter app using breeze on live server
- How to get the random value form a specific column in laravel ?
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- Send post data from controller to view
- Best Practices for Error Handling in Production Server Code (example code)
- How to pass data to partial view file in laravel
- How to pass data to multiple partial view files in laravel
- InRandomOrder() method with example in laravel
- Get current URL on visit URL in Laravel
- How to create projects method with belongstomany relationship in user model
- How to create controller in laravel
- Docker important commands to run laravel application with docker
- How to Access Array in blade laravel
- Extra Filter Query on Relationships in Laravel
- How to add background image to div using Tailwindcss, Vite in Laravel Environment