vendredi 14 juin 2019

Laravel custom validation to all model

I am using laravel latest version and i have a common field in all model called slug.I would to check whether slug is unique or not. i have slug field in all tables

so i have extended Valdiator class

class CustomValidator extends Validator{

protected function validateIsUniqueSlug($attribute, $value, $parameters)
    {

        $isSlugExist= User::where('slug', $value)->exists();
        if ($isSlugExist) {
            return false;
        }
        return true;
    }

}

this works but problem here is i need to repeat this for models but i dont want to do this .is there any better approach so i can handle it in one method

i know laravel has sluggable package but for some reason i cant use that package



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire