我有一系列的收藏。在收藏中我有关系。我如何使用 whereIn
在这些关系中?
$arrayCollections = $category->products;
$arrayCollections = $arrayCollections->character->whereIn('id', [1,2,3,4]); //I need use whereIn in character relation. I get error: Property [character] does not exist on this collection instance.
foreach($arrayCollections as $product) {
.... //but in foreach I can use $product->character
}
与产品型号的关系::
public function character()
{
return $this->hasMany('App\Models\ProductCharacter');
}
最佳答案
尝试这个:
$characters = $category->products()->character()->whereIn('id', [1,2,3,4])->get();
foreach($characters as $character) {
// $character
}
关于php - Laravel whereIn 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52474370/