lundi 1 juillet 2019

Using Eloquent's HasMany relationship with whereIn

I have a user model with an array of identifiers. An identifier can be either an IP address or something else. Users can be banned, and this is done through their identifiers. However, I cannot seem to accomplish such logic using Eloquent's HasMany relationship.

I want that, as long as there's a Ban model (entry) where the identifier matches one of the identifiers in the user's identifiers-array, then it should be added to that hasMany relationship.

I've tried to use "HasMany" with "whereIn", but that did not work as it will still look for the foreign key relationship, then apply the "whereIn" constraints. This is not what I want.

Here's how I try to define the user's relationship with bans.

public function bans()
{
    // Due to how banning works, there might exist a ban record for each of the player's identifier (steam, ip address
    // rockstar license, etc), and it's important to get all.

    // all ban records where the identifier column equals one of the user's identifiers (array)
    return $this->hasMany(Ban::class)->whereIn('identifier', $this->identifiers);
}

What is expected is that it should return all the ban records which has one of the user's identifiers in its identifier column. But this doesn't work as the HasMany still applies foreign key equality check.

Thanks in advance!



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire