it-brains / laravel-validator
0.1.10
2023-03-14 21:52 UTC
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
自定义规则
- uniqueModel
- existsModel
示例
uniqueModel 替代 just unique
替代
...
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']),
...
];
}
...
或使用模型类中的表
...
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 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']),
...
],
];
}
...