
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
- Call to undefined relationship [user] on model [App\Models\Post]
- Permission denied error while creating storage link in Laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- How to get data from two tables in laravel
- SQLSTATE[42S22]: Column not found: 1054 Unknown column 'users.post_id' in 'where clause
- SQLSTATE[42S02]: Base table or view not found: 1146 Table 'laravel8.projects' doesn't exist
- How to insert dynamic value to additional column in pivot table in laravel
- Undefined property: stdClass::$title
- Add a subselect based on relationship using withAggregate method
- Laravel order by date not working
- Laravel csrf token mismatch for ajax POST Request
- Extract only time from datetime in laravel
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Always load the relationship data with eager loading in Laravel
- On delete set foreign id column value null using migration in laravel 8
- Remove several global scope from query
- How to pass query string with pagination in laravel
- How to print form data in laravel
- How to add active class to menu item in laravel
- Drop foreign key column in Laravel using migration
- Laravel route redirect not working
- Update if exist else insert new record in laravel
- Retain selected value of select box in Laravel
- How to include header file in laravel
- Root composer.json requires php ^7.3 but your php version (8.0.0) does not satisfy that requirement