1

I need all users data in one blade, I have table1 and table2.

table1

'id','name','phone','email','status','created_at'

table2

'id','name','mobile','email','status','created_at'

I tried to do this but not an expected result.

 $usersTbl = DB::table('table1')->select('id','name','phone','email','status','created_at')->groupBy('phone')->paginate(20);

 $ordersTbl = DB::table('table2')->select('id','name','mobile','email','status','created_at')->groupBy('mobile')->paginate(20);

 $items=$ordersTbl->merge($usersTbl);
 dd($items);exit;
5
  • What's the end goal? are they going to be in 1 big array or are you looking for something else? Commented Dec 26, 2019 at 7:06
  • once you fetch the data from both tables you can merge the result instead of the merging table Commented Dec 26, 2019 at 7:07
  • maybe you need to use union, so you can merge using SQL query instead laravel/php Commented Dec 26, 2019 at 7:07
  • @AmitSharma can you give me example how to merge it Commented Dec 26, 2019 at 7:16
  • see in the answers Commented Dec 26, 2019 at 7:28

2 Answers 2

4

You have pagination, if you merge it the pagination need to recalculate.

You need to use unionAll and then paginate:

$usersTbl = DB::table('table1')->select('id','name','phone','email','status','created_at')->groupBy('phone');

$ordersTbl = DB::table('table2')->select('id','name','mobile AS phone','email','status','created_at')->groupBy('phone');

$mergeTbl = $usersTbl->unionAll($ordersTbl);
DB::table(DB::raw("({$mergeTbl->toSql()}) AS mg"))->mergeBindings($mergeTbl)->paginate(20);
Sign up to request clarification or add additional context in comments.

5 Comments

(1/1) BadMethodCallException Method Illuminate\Support\Collection::unionAll does not exist.
@WorldsHero Do you get them or use paginate in usersTbl?
use paginate in usersTbl
@WorldsHero don't use it. Follow my code, groupBy them and then unionAll them, and then paginate.
-1

Try This Method:

public function Details($id) {
    /** Make an empty array **/
        $patients = array();
    /** Get Data from first model **/
        $user_details = User::where(['id' => $id])->first();
    /** Get Data from second model **/
        $profile_details = Profile::where(['id' => $id])->first();
        $patients[] = $user_details; 
        $patients[] = $profile_details; 
        }
        return view('custom-view', compact('patients'));
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.