
Retrieve count of nested relationship data in Laravel
Retrieve count of nested relationship data in Laravel
We will use programming in this lesson to attempt to solve the "Retrieve count of nested relationship data in Laravel".
Sometimes you have to retrieve count of nested relationship data in Laravel. In my case, I have to get the number of orders placed for every product, this code snippet help me to find out the solution.-
Return products with number of orders using count of nested relationship data
$products = Product::all(); $productsArr = $products->map(function (Product $product){ $productObj = $product->toArray(); $productObj['orders_count'] = $product->orders()->count(); return $productObj; });
This code snippet will help you to get the total number of orders (records of relationship data) with Product. In this code there is a map method which get the count of orders form the relationship while iterating product model data.
You have to create the orders method in Product model.
app\Models\Product.php
public function orders(){ return $this->hasMany('App\Models\Order'); }
-
Return products with number of orders using eager load relationship
$products = Product::with('orders')->get(); $productsArr = $products->map(function (Product $product){ $productObj = $product->toArray(); $productObj['orders_count'] = $product->orders->count(); return $productObj; });
It helps you to get the total number of orders (records of relationship data) with Product. In this code there is a map method which get the count of orders form the relationship while iterating products.
Here we have use eager load relationship which helps you to reduce the number of query while getting data from table.
-
Return products with number of orders using withCount() function
$products = Product::withCount('orders')->get(); $productsArr = $products->map(function (Product $product){ $productObj = $product->toArray(); $productObj['orders_count'] = $product ->getAttribute('orders_count'); return $productObj; });
This code snippet will help you to get the total number of orders (records of relationship data) with product. There is a map method which get the count of orders form the relationship data while iterating products.
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
- Return view from route Laravel
- Trying to access array offset on value of type null error in laravel
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- Laravel migration add foreign key to existing table
- Ajax GET request in laravel
- Json encode method in laravel
- How to create pivot table in laravel using migration
- Redirect to previous page or url in laravel
- Eager loading dynamically in laravel
- Store logged in user details in session and display in view in laravel
- How to pass data to multiple partial view files in laravel
- Property [user] does not exist on this collection instance
- How to start websocket server in laravel
- How to display a specific word from a string in laravel
- Add class to body in laravel view
- Generate random string lowercase in Laravel
- Method Illuminate\Http\Request::validated does not exist
- Call to undefined method Illuminate\Support\Facades\Request::all()
- How to upload image in laravel 8
- Display success message in laravel
- Display option of select as selected with blade directive Laravel
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- How to decrypt laravel password
- Update email with unique validation in laravel
- Class "App\Http\Controllers\Auth\Verified" not found