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
- Fatal error: Composer detected issues in your platform: Your Composer dependencies require a PHP version ">= 8.0.0"
- How to pass data to partial view file in laravel
- How to pass query string with pagination in laravel
- Undefined property: stdClass::$title
- File_put_contents(/var/www/html/w3code/storage/framework/sessions/CXwN3EXKxERD6jgy3rADcaAAbAx8FRKih2JK7UO9): Failed to open stream: Permission denied
- How to display validation error in laravel
- How to create belongstomany relation using custom name on custom pivot table
- Run artisan command to generate key in laravel
- How to get user information using hootlex/laravel-friendships package in laravel
- Return redirect laravel not working
- How to get last month records in Laravel
- Call to undefined method App\Models\User::follow()
- Seed database using SQL file in Laravel
- Get Array of IDs from Eloquent Collection
- Json encode method in laravel
- Get today records in Laravel
- How to create and run user seeder in laravel
- Get the post details if it has at least one comment in comments table
- Input file with max size validation in laravel
- Array to string conversion laravel Controller
- SQLSTATE[23000]: Integrity constraint violation: 1022 Can't write; duplicate key in table
- How to create new user without form submission in laravel
- Wheredate in laravel not working
- Get last year created records in Laravel
- Permanently delete a record in laravel