it-brains / laravel-validator
Installs: 17 444
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 2
Forks: 0
Open Issues: 0
Requires
- php: >=7.0
- illuminate/database: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
- illuminate/validation: ^5.0|^6.0|^7.0|^8.0|^9.0|^10.0
README
Custom rules
- uniqueModel
- existsModel
Examples
uniqueModel instead just unique
Instead of
...
use Illuminate\Validation\Rule;
...
public function rules()
{
return [
'title' => [
Rule::unique('containers', 'name')
->where('user_id', $this->user()->id)
->whereNull('deleted_at'),
],
// other rules
'type' => Rule::in(['A', 'B']),
...
];
}
...
or with table from model's class
...
use App\Container;
use Illuminate\Validation\Rule;
...
public function rules()
{
return [
'title' => [
Rule::unique((new Container::class)->getTable(), 'name')
->where('user_id', $this->user()->id)
->whereNull('deleted_at'),
],
// other rules
'type' => Rule::in(['A', 'B']),
...
];
}
...
Use next
...
use App\Container;
use ITBrains\Validation\Rule;
...
public function rules()
{
return [
'title' => [
Rule::uniqueModel(Container::class, 'name')
->where('user_id', $this->user()->id),
// other rules
'type' => Rule::in(['A', 'B']),
...
],
];
}
...