Aiden
Aiden

Reputation: 399

Eloquent relationship with LIKE comparison

If I want an eloquent relationship e.g.

public function subscribed(){

    return $this->hasOne('App\Models\SuppressionList', 'email', 'email');

}

This makes a strict equality comparison so if the email attribute on the primary table was [email protected] and the email attribute on the foreign table was [email protected]. Is there any way to create a loose relationship so that it would join records with a LIKE comparator opposed to =.

Cheers

Upvotes: 1

Views: 199

Answers (1)

Josh Rumbut
Josh Rumbut

Reputation: 2710

Maybe this makes more of your example case than i should, but, emails, at least provided by most providers, are often more than just case insensitive. They also usually ignore the . in the username, maybe something else I'm forgetting.

In cases like this, it's often a good idea to have two fields, display_email (what the user entered, if presentation matters), and canonical_email, which has the email stripped of periods in the username, all lowercase, etc. This can save some CPU cycles when joining large tables (versus doing the transformation each time), and helps you handle a user who types in their email slightly differently each time.

Upvotes: 2

Related Questions