Hamza Qureshi
Hamza Qureshi

Reputation: 202

how to merge 2 same collections in larave

consider on first query i get 2 rows and on 2nd query i get 1 row i want to 2+1 = 3 rows into a single collection so that i can have all the data here is my query

 $invoices = Invoice::whereIn('user_id', $ids)->get(); //have data in column id 1 and 2
    $stock = Invoice::where('customer_id' , Auth::id())->get(); // have data in column id 3 
    $merge = //how i can merge this?

now consider i merged both my output should be using foreach

foreach($merge as $a){
$b[] = $a->id;
}
dd($b);

output should be [1,2,3]

Upvotes: 0

Views: 50

Answers (2)

lagbox
lagbox

Reputation: 50481

You can do this in one query, BUT if you actually want to merge these 2 Eloquent Collections:

$new = $invoices->merge($stock);

Surprise, there is a merge method.

Laravel 8.x Docs - Collections - Available Methods - merge

Upvotes: 2

OMR
OMR

Reputation: 12188

you can get the both result in one query using orWhere:

$mergedResult=Invoice::whereIn('user_id', $ids)->orWhere('customer_id' , Auth::id())->get();

Upvotes: 2

Related Questions