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
- Global scope in Laravel with example
- How to insert dynamic values to additional column with pivot column in pivot table on multiple records
- Call to undefined function Illuminate\Encryption\openssl_cipher_iv_length()
- Create project table with model and migration
- Sample .htaccess file and index.php file under public directory in laravel
- Laravel csrf token mismatch for ajax POST Request
- Laravel 11 step by step instructions to upload file in storage directory and display in blade file
- Get content from web URL in laravel
- Send OTP using textlocal api in laravel
- Target class [App\Http\Controllers\Auth\Request] does not exist.
- Multiple Level eager loading in Laravel
- Laravel 9 route group with controller
- How to get the random value form a specific column in laravel ?
- Display first n record from collection in laravel view
- Pass value from controller to model in laravel
- Class "App\Http\Controllers\Auth\Verified" not found
- How to pass data to partial view file in laravel
- Get domain name in laravel
- How to customize pagination view in laravel
- How to get tomorrow and yesterday date in laravel
- Trying to access array offset on value of type null error in laravel
- Recursive function example code PHP Laravel
- Route not defined in Laravel
- External link not working in laravel blade
- Call to a member function pluck() on array