
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; });
0This 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; });
0It 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; });
0This 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.
Random Code Snippet Queries: Laravel
- How to avoid duplicate entries in pivot table in Laravel
- JQuery each loop on json response after ajax in laravel
- Get last record from table in laravel
- How to restore multiple records after soft-deletes in Laravel
- How to get file extension from input type file in laravel
- How to get specific columns using Laravel eloquent methods
- Global scope in Laravel with example
- Seed database using SQL file in Laravel
- Laravel order by date not working
- How to return a column with different name in Laravel
- The Pusher library requires the PHP cURL module. Please ensure it is installed
- How to check record exist or not in relationship table
- Get id of last inserted record in laravel
- How to display order by null last in laravel
- How to get query string value in laravel
- OrderBy on Eloquent relationships method in Laravel
- Define variable and use in Laravel controller method
- Display first n record from collection in laravel view
- Ignore Records where a field has NULL value in Laravel
- How to get list of all views file in laravel
- How to add foreign key in laravel using migration
- How to pass data to route in laravel?
- Validation for multiple forms on same page in laravel
- How to upload image in laravel 8
- How to start websocket server in laravel