Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
168 views
in Technique[技术] by (71.8m points)

php - 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]

question from:https://stackoverflow.com/questions/65942825/how-to-merge-2-same-collections-in-larave

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...