Laravel Merge Multiple collections Object And Array

Today, We want to share with you Laravel Merge Multiple collections Object And Array.In this post we will show you merge Laravel objects in controller, hear for Laravel 5.7 Collections: Merging Multiple Collection Values we will give you demo and example for implement.In this post, we will learn about Laravel how merge two query result into single object with an example.

Laravel Merge Multiple collections Object And Array

There are the Following The simple About Laravel Merge Multiple collections Object And Array Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to merge Laravel objects in controller, so the Merge 2 collections into one using Laravel 5.7 for this example is following below.

Read Also:  PHP crud Mysqli Select Insert Update and Delete query

Laravel Merge Multiple collections in controller

$apps = DB::table("products")
->join('categories', 'products.category_id', '=', 'categories.id')
->select('products.category_id','categories.name', DB::raw('COUNT(products.category_id) as totalrow'))
->groupBy('products.category_id')
->get();


$subcategories = DB::table('products')
->join('subcategories', 'products.sub_category_id', '=', 'subcategories.id')
->select('products.sub_category_id','subcategories.name', DB::raw('COUNT(products.sub_category_id) as count'))
->groupBy('sub_category_id')
->get();


$new_collection = $apps->merge($subcategories);
dd($new_collection);

Collection {#994 ▼
  #items: array:8 [▼
    0 => {#932 ▼
      +"category_id": 1
      +"name": "Pakainfo Blog System"
      +"totalrow": 4
    }
    1 => {#933 ▼
      +"category_id": 2
      +"name": "Ministackoverflow Question and Answer"
      +"totalrow": 20
    }
    2 => {#934 ▼
      +"category_id": 3
      +"name": "Discuss Mangement System"
      +"totalrow": 5
    }
    3 => {#935 ▼
      +"category_id": 4
      +"name": "Infinity Blog System"
      +"totalrow": 5
    }
    4 => {#988 ▼
      +"sub_category_id": 2
      +"name": "Padding Process"
      +"count": 28
    }
    5 => {#989 ▼
      +"sub_category_id": 4
      +"name": "Padding Process"
      +"count": 1
    }
    6 => {#990 ▼
      +"sub_category_id": 6
      +"name": "Need More help"
      +"count": 4
    }
    7 => {#991 ▼
      +"sub_category_id": 8
      +"name": "Need More help"
      +"count": 1
    }
  ]
}

merge Laravel Array in controller

$apps = DB::table("products")
->join('categories', 'products.category_id', '=', 'categories.id')
->select('products.category_id','categories.name', DB::raw('COUNT(products.category_id) as totalrow'))
->groupBy('products.category_id')
->get();


$subcategories = DB::table('products')
->join('subcategories', 'products.sub_category_id', '=', 'subcategories.id')
->select('products.sub_category_id','subcategories.name', DB::raw('COUNT(products.sub_category_id) as count'))
->groupBy('sub_category_id')
->get();
//dd($subcategories);

$array = array_merge($apps->toArray(), $subcategories->toArray());
dd($array);


array:8 [▼
  0 => {#932 ▼
    +"category_id": 1
    +"name": "Pakainfo Blog System"
    +"totalrow": 4
  }
  1 => {#933 ▼
    +"category_id": 2
    +"name": "Ministackoverflow Question and Answer"
    +"totalrow": 20
  }
  2 => {#934 ▼
    +"category_id": 3
    +"name": "Discuss Mangement System"
    +"totalrow": 5
  }
  3 => {#935 ▼
    +"category_id": 4
    +"name": "Infinity Blog System"
    +"totalrow": 5
  }
  4 => {#988 ▼
    +"sub_category_id": 2
    +"name": "Padding Process"
    +"count": 28
  }
  5 => {#989 ▼
    +"sub_category_id": 4
    +"name": "Padding Process"
    +"count": 1
  }
  6 => {#990 ▼
    +"sub_category_id": 6
    +"name": "Need More help"
    +"count": 4
  }
  7 => {#991 ▼
    +"sub_category_id": 8
    +"name": "Need More help"
    +"count": 1
  }
]

Angular 6 CRUD Operations Application Tutorials

Read :

Read Also:  registration and signin form in php and mysql with validation

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Laravel Merge Multiple collections Object And Array.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.