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

·

,
Laravel 7.2 routing with route group auth guard check with prefix

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

Comments

One response to “How to merge two laravel mysql queries data in one json?”

  1. alexa Avatar
    alexa

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