LaravelMysql

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

merge two laravel mysql queries data in one json

Laravel
581Views

Hello to all, welcome to therichpost.com. 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')
            ->get();
    	
    	$list2 = DB::table('works')
            ->select('id', 'staff_id')
            ->get();

      $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. Therichpost.com

Ajay Malhotra
the authorAjay Malhotra
Hello to all. Welcome to therichpost.com. 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
    end)
    WHERE `id` in (184, 185)

Leave a Reply

Positive SSL