
Add a subselect based on relationship using withAggregate method
Add a subselect based on relationship using withAggregate method
Good day, guys. In this post, we’ll look at how to solve the "Add a subselect based on relationship using withAggregate method" programming puzzle.
You can add a subselect based on a relationship using the withAggregate method in Laravel. Sometimes you have to get the data from a column of another table like if you want to get a post from the posts table with the column data user’s name from users table. In that case the withAggregate method helps you to get the data from a column of another table.-
Add a subselect based on relationship using withAggregate in Laravel
Syntax :
$posts = App\Models\Post::withAggregate('user', 'email')->first();
Code Example : //app\Models\Post.php
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function user() { return $this->belongsTo(User::class); } }
routes\web.php
Route::get('/subselect-with-aggregate', function(){ $posts = App\Models\Post::withAggregate('user', 'email')->first(); dd($posts); });
Output :
[▼
"id" => 11
"title" => "Labore do commodi et"
"body" => "Lorem ipsum"
"user_id" => 1
"created_at" => "2021-10-13 05:10:23"
"updated_at" => "2021-10-13 05:10:23"
"user_email" => "[email protected]"
]You can get the email of user who created the post using user_email after getting the data from table.
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
- Get latest record by created at in Laravel
- Laravel 9 pagination with search filter
- Split an Eloquent Collection by half in Laravel
- Argument 1 passed to Illuminate\Database\Query\Builder::cleanBindings() must be of the type array, null given
- Laravel recursive function in controller
- Order by multiple columns in Laravel
- Laravel create table migration with model
- Insert values in pivot table dynamically in laravel
- Laravel delete all rows older than 30 days
- Display data in table using foreach in Laravel
- How to delete record in Laravel with ajax
- How to use or operator in laravel
- How to call controller function from view in Laravel
- Ajax GET request in laravel
- First and last item of the array using foreach iteration in laravel blade
- How to pass two variables in HREF in laravel
- Laravel API response format
- Insert dummy data in users table Laravel
- Get today records in Laravel
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Get last week data in Laravel
- Laravel get single row by id
- How to update record after save method in Laravel
- How to remove package from laravel
- Route group with URI prefix using middleware and route name prefixes