Laravel whereIn in array

I have array of collections. In collections I have relation. How I can use whereIn in these relations?

$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
}

Relation in product model::

public function character()
{
    return $this->hasMany('App\Models\ProductCharacter');
}

Try this:

$characters = $category->products()->character()->whereIn('id', [1,2,3,4])->get();

foreach($characters as $character) {
    // $character
}

You seem to need an indirect relationship. The best way to get this is by using hasManyThrough

In your Category model

public function characters() {
     return $this->hasManyThrough(Character::class, Product::class); // Has many characters through products
}

Then you can directly do:

 $characters = $category->characters()->whereIn('id', [ 1, 2, 3, 4 ])->get();

what about this ? I added first() :

$characters = $category->products()->first()->character()->whereIn('id', [1,2,3,4])->get();

foreach($characters as $character) {
    // $character
}

You can try this

$arrayCollections = $category->products()->with('character')->whereIn('id' ,  [1,2,3,4])->get();

foreach($arrayCollections as $product) {}

You should use the whereHas() methode and with() method.

$arrayCollections = $category->products()
    ->whereHas('character', function($character){
        $character->whereIn('id' ,  [1,2,3,4]);
    })
    ->with('character')->get();

This will get you a collection of "products" that have "characters" attach to it that have ID in [1,2,3,4].