“Valores únicos de matriz de validación de Laravel” Código de respuesta

Obtenga valores únicos en Laravel

$users = User::select('name')->distinct()->get();
Precious Porpoise

Valores únicos de matriz de validación de Laravel

<?php
$validator = Validator::make($request->all(), [
  'myArray' => ['required', 'array', 'min:1', 'max:100', 'distinct'],
  'myArray.*' => ['required', 'integer', 'min:1', 'max:999999']
]);

if ($validator->fails()) {
  return [
    'status' => 400,
    'response' => $validator->errors()
  ];
}

$validated = $validator->validated();

// distinct is there for the uniqunes
Realy Silly Shark

Laravel: Validación única en la actualización

Just a side note, most answers to this question talk about email_address while in Laravel's inbuilt auth system, the email field name is just email. Here is an example how you can validate a unique field, i.e. an email on the update:

In a Form Request, you do like this:

public function rules()
{
  return [
      'email' => 'required|email|unique:users,email,'.$this->user->id,
  ];
}
Or if you are validating your data in a controller directly:

public function update(Request $request, User $user)
{
  $request->validate([
      'email' => 'required|email|unique:users,email,'.$user->id,
  ]);
}
Update: If you are updating the signed in user and aren't injecting the User model into your route, you may encounter undefined property when accessing id on $this->user. In that case, use:

public function rules()
    {
      return [
          'email' => 'required|email|unique:users,email,'.$this->user()->id,
      ];
    }
A more elegant way since Laravel 5.7 is:

public function rules()
{
    return [
        'email' => ['required', 'email', \Illuminate\Validation\Rule::unique('users')->ignore($this->user()->id)]
    ];
}
shafeeque

Laravel Validato de datos Forma una matriz única

'username' => ['required', Rule::unique('users', 'username')],
DJ Ultimate Disco Party

Respuestas similares a “Valores únicos de matriz de validación de Laravel”

Preguntas similares a “Valores únicos de matriz de validación de Laravel”

Más respuestas relacionadas con “Valores únicos de matriz de validación de Laravel” en PHP

Explore las respuestas de código populares por idioma

Explorar otros lenguajes de código