
How to get specific columns using with method in laravel Eloquent relationship
How to get specific columns using with method in laravel Eloquent relationship
In this tutorial, we will try to find the solution to "How to get specific columns using with method in laravel Eloquent relationship" through programming.
-
Select specific columns using with() method on Eloquent relationships in laravel
--PATH app\Http\Controllers\<YourController.php>$data = Post::where('status', 1) ->with(['comments' => function($query) { $query->select('id','body'); }])->get();
To use with() method in Laravel, models need to be already connected (one to many, many to many...) with relationships. If they are not connected please Check here to how to make relation. We have to first place a comments method on the Post model to define the relationship between these two models. Now we can select specific columns from the comments table which we want to select using the select method on query builder.
By default with() method gives you all fields from tables which in many cases we do not need. So using this code snippet we can select the specific column from the comments table which is required to us. This code snippet returns all the posts with all related comments with particular columns to every post which status is 1.
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
- Remove array keys and values if it does not exist in other array in Laravel
- How to get records in random order in laravel
- How to check records exist in loaded relationship in Laravel blade view
- How to check record exist or not in relationship table
- Add class to body in laravel view
- How to display order by null last in laravel
- How to get all posts which contains comments in laravel
- Get today records in Laravel
- Connection could not be established with host smtp.gmail.com :stream_socket_client(): unable to connect to tcp://smtp.gmail.com:587 (Connection refused)"
- How to remove P tag from CkEditor in Laravel?
- Drop foreign key column in Laravel using migration
- Update existing pivot table data in laravel
- Laravel csrf token mismatch for ajax POST Request
- Retain selected value of select box in Laravel
- Symlink(): No such file or directory
- Get content from web URL in laravel
- How to Access Array in blade laravel
- How to get route name on visit URL in laravel
- Create model with migration and seeder
- Declaration of App\Models\Post::sluggable() must be compatible with Cviebrock\EloquentSluggable\Sluggable
- Define variable and use in Laravel controller method
- How to get count of all records created at yesterday
- Create record with unique slug in laravel
- Insert dummy data in users table Laravel
- Laravel change date format