php - Laravel whereIn 数组

标签 php laravel

我有一系列的收藏。在收藏中我有关系。我如何使用 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/

相关文章:

laravel - 按 Eloquent 访问器字段在 Yajrabox 数据表中排序

php - Laravel 如何在 Blade 中将分钟转换为天?

php - 如何使用 Azure PHP SDK 通过分块来上传大块 blob?

php - 如果行为空,则回显默认文本,否则回显显示数据

PHP 和 JQuery : value not updating

php - Zend 框架 : Using Dashes in Controller Names/URLs

laravel - 我无法在我的 laravel 项目中安装视频缩略图

php - 如何在 Laravel 8 中的搜索框中过滤 bool 值

laravel - 在 laravel 5.7 中更改验证电子邮件中验证链接中自定义参数的 “VerifyEmail” 类,用于验证电子邮件中的方法 verificationUrl()

php - 获取页面标题的正则表达式