
Get products with number of orders in Laravel
Get products with number of orders in Laravel
In this article, we will see how to solve "Get products with number of orders in Laravel".
You can get the products with number of orders in Laravel. Sometimes you have to get the number of orders placed for the products, in that case, this code snippet will help you to find out the solution for you.-
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
- SQLSTATE[42000]: Syntax error or access violation: 1075 Incorrect table definition; there can be only one auto column and it must be defined as a key
- How to validate form input data in laravel
- How to get the id of last record from collection object in laravel view
- How to get route method name in Laravel
- Redirect to another view from controller in laravel
- In order to use the Auth::routes() method, please install the laravel/ui package
- How to validate website url in laravel using validaiton
- Laravel API response format
- SQLSTATE[42000]: Syntax error or access violation: 1055
- Remove several global scope from query
- Extra Filter Query on Relationships in Laravel
- Php artisan make model, factory, migration and controller in single command
- Retrieve count of nested relationship data in Laravel
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- Page loader in laravel
- How to get IP address in laravel
- Cast Array to an Object in Controller and then pass to view in laravel
- How to insert multiple rows in mysql using loop in laravel?
- Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails
- How to check duplicate entry in laravel
- Get 30 days older records from table in laravel
- Get laravel version
- Method Illuminate\Http\Request::validated does not exist
- How to display pivot table column value in laravel
- Conditional where clause in Laravel