How to merge two laravel mysql queries data in one json?

merge two laravel mysql queries data in one json


Hello to all, welcome to In this post, I will tell you, How to merge two laravel mysql queries data in one json?  Laravel is one of the top php mvc framework

In this post example, I will have two mysql queries and both two mysql queries data will be get in json in one variable.

Here is the working code and I added this code in laravel controller file:

public function getuserdata(){
    	$finalArray = [];

    	$list1 = DB::table('users')
            ->select('id', 'name')
    	$list2 = DB::table('works')
            ->select('id', 'staff_id')

      $arr1 = [];
      foreach ($list1 as $key => $value) {
        $arr1[] = $value;
      $arr2 = [];
      foreach ($list2 as $key => $value) {
        $arr2[] = $value;
      $finalArray = array_merge($arr1, $arr2);
      return response()->json($finalArray);

I have used $finalArray  to store both mysql queries data.

 There are so many code tricks in laravel and i will let you know all. Please do comment if you any query related to this post. Thank you.

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to Myself Ajay Malhotra and I am full stack developer. I love coding. I know Wordpress, Core php, Angularjs, Angular 2, Angular 6, Angular 7, laravel, Codeigniter, Shopify, Squarespace, Jquery, Google Map Api, Vuejs, Reactjs, Bigcommerce etc.

1 Comment

  • UPDATE `working-hours`
    SET `status` = (case when `place_id` = 1 then 1
    when `place_id` = 2 then 0
    WHERE `id` in (184, 185)

Leave a Reply

Positive SSL