【发布时间】:2020-04-03 12:56:52
【问题描述】:
我正在使用 Laravel,我有两个包含产品 ID 的不同集合
第一个是colorProduct,第二个是tagProduct
所以我想比较这两者并只获得相同的产品 ID,我该怎么做呢?
$colorProducts = Color::where('code', $request->color)->get()->first()->products;
$tagProducts = $tag->products->where('shop_id', $shop->id);
$colorAndTagProducts = collect();
foreach ($colorProducts->toBase()->merge($tagProducts)->unique('id')->groupBy('id') as $allProducts) {
if ($allProducts->count() >= 1) {
$colorAndTagProducts[] = $allProducts->first();
}
}
这里
$colorAndTagProducts
给我所有记录来自两个集合,但我只想要相同的记录
【问题讨论】:
标签: php laravel collections