PHP code example of h-farm / laravel-email-domain-rule

1. Go to this page and download the library: Download h-farm/laravel-email-domain-rule library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

h-farm / laravel-email-domain-rule example snippets


return [

    /*
    |--------------------------------------------------------------------------
    | Email Domain model
    |--------------------------------------------------------------------------
    |
    | Here you may specify the fully qualified class name of the email domain model.
    |
    */

    'email_domain_model' => Maize\EmailDomainRule\Models\EmailDomain::class,

    /*
    |--------------------------------------------------------------------------
    | Email Domain wildcard
    |--------------------------------------------------------------------------
    |
    | Here you may specify the character used as wildcard for all email domains.
    |
    */

    'email_domain_wildcard' => '*',

    /*
    |--------------------------------------------------------------------------
    | Validation message
    |--------------------------------------------------------------------------
    |
    | Here you may specify the message thrown if the validation rule fails.
    |
    */

    'validation_message' => 'The selected :attribute does not have a valid domain.',
];

use Maize\EmailDomainRule\EmailDomainRule;
use Illuminate\Support\Facades\Validator;

$email = '[email protected]';

Validator::make([
    'email' => $email,
], [
    'email' => [
        'string',
        'email',
        new EmailDomainRule,
    ],
])->validated(); 

use Maize\EmailDomainRule\EmailDomainRule;
use Maize\EmailDomainRule\Models\EmailDomain;
use Illuminate\Support\Facades\Validator;

EmailDomain::create(['domain' => '*.example.com']);

Validator::make([
    'email' => '[email protected]',
], [
    'email' => ['string', 'email', new EmailDomainRule],
])->fails(); // returns true as the given domain is not in the list

Validator::make([
    'email' => '[email protected]',
], [
    'email' => ['string', 'email', new EmailDomainRule],
])->fails(); // returns false as the given domain matches the wildcard domain

use Maize\EmailDomainRule\EmailDomainRule as BaseEmailDomain;
use Illuminate\Database\Eloquent\Builder;

class EmailDomain extends BaseEmailDomain
{
    protected $fillable = [
        'domain',
        'tenant_id',
    ];

    protected static function booted()
    {
        static::addGlobalScope('tenantAware', function (Builder $builder) {
            $builder->where('tenant_id', auth()->user()->tenant_id);
        });
    }
}
bash
php artisan vendor:publish --provider="Maize\EmailDomainRule\EmailDomainRuleServiceProvider" --tag="email-domain-rule-migrations"
php artisan migrate
bash
php artisan vendor:publish --provider="Maize\EmailDomainRule\EmailDomainRuleServiceProvider" --tag="email-domain-rule-config"