Categories

Saturday, April 20, 2024
#919814419350 therichposts@gmail.com
LaravelMysql

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

Laravel 7.2 routing with route group auth guard check with prefix

merge two laravel mysql queries data in one json

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

therichpost
the authortherichpost
Hello to all. Welcome to therichpost.com. Myself Ajay Malhotra and I am freelance full stack developer. I love coding. I know WordPress, Core php, Angularjs, Angular 14, Angular 15, Angular 16, Angular 17, Bootstrap 5, Nodejs, Laravel, Codeigniter, Shopify, Squarespace, jQuery, Google Map Api, Vuejs, Reactjs, Big commerce etc.

1 Comment

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.