it-brains / laravel-validator by dragg

17,444
0
2
Package Data
Maintainer Username: dragg
Maintainer Contact: dragg.ko@gmail.com (Nikolay Zhidenko)
Package Create Date: 2017-02-10
Package Last Update: 2023-03-14
Language: PHP
License: MIT
Last Refreshed: 2024-11-21 03:00:11
Package Statistics
Total Downloads: 17,444
Monthly Downloads: 96
Daily Downloads: 0
Total Stars: 0
Total Watchers: 2
Total Forks: 0
Total Open Issues: 0

Useful Laravel Validation Rules

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']),
            ...
        ],
    ];
}

...