I wanted to know how to validate fields only ones (multiple). for example. I have a Permission
model that has a restriction, in which two fields are unique.
Permission migration
Schema::create('permissions', function (Blueprint $table) {
$table->increments('id');
$table->string('namespace')->nullable();
$table->string('name');
$table->unique(array('namespace', 'name'));
});
I know that the Validator
class has a unique rule, but I think it is only for a field, not two or more.
class Permission extends BaseModel {
protected $table = 'permissions';
public static $rules = array(
'name' => 'required|alpha',
'namespace' => 'alpha'
);
}
class BaseModel extends Eloquent
{
public $errors;
public static function boot()
{
parent::boot();
static::saving(function($model)
{
return $model->validate();
});
}
public function validate()
{
$validation = Validator::make($this->attributes, static::$rules);
if($validation->passes()) return true;
$this->errors = $validation->messages();
return false;
}
}
my question was how to validate these situations, where there is more than one field in the db unique constraint.
There's no native way to validate that. If you look at Custom Validators and the Uniqueness Validation, you should be able to come up with a solution suited for your needs.
If you don't need something generic you can take look at this example.