This time I will bring you the unique and exists verification rules in LaravelDetailed explanation of the optimization steps, what are the notes for optimizing the unique and exists verification rules in Laravel, and the following are practical cases , let’s take a look.
Preface
Laravel provides a variety of methods to validate application input data. By default, Laravel's It is very convenient to verify requests through the ValidatesRequests trait in Laravel, and it is automatically introduced in the BaseController class. The two rules exitsts() and unique() are very powerful and convenient. They need to verify the existing data in the database during use. Usually they will be written like the following:// exists example 'email' => 'exists:staff,account_id,1' // unique example 'email' => 'unique:users,email_address,$user->id,id,account_id,1'
'email' => [ 'required', Rule::exists('staff')->where(function ($query) { $query->where('account_id', 1); }), ],
'email' => [ 'required', Rule::unique('users')->ignore($user->id)->where(function ($query) { $query->where('account_id', 1); }) ],
protected function formatWheres() { return collect($this->wheres)->map(function ($where) { return $where['column'].','.$where['value']; })->implode(','); }
Detailed explanation of the use of PHP doubly linked list
Detailed explanation of the steps for PHP to use foreach to convert an array
The above is the detailed content of Detailed explanation of optimization steps for unique and exists validation rules in Laravel. For more information, please follow other related articles on the PHP Chinese website!