Home > other >  Laravel validation unique ignore lowercase or uppercase
Laravel validation unique ignore lowercase or uppercase

Time:09-17

I made a unique validation in 1 field, so for example I input "pizza", while the "pizza" already exists, so it automatically doesn't work but if I input "Pizza" the letter "p" is large it still passes, how do I fix it?

so I want when validating he doesn't pay attention lowercase or uppercase.

This my code now :

$exists = Category::where(\Str::lower('name'), '=', \Str::lower(request('name')))->orderBy('name')->first();
            if ($exists) {
                return redirect()->route('categories.index')->with('error', 'Nama kategori sudah ada');
            } else {
                Category::create(request()->all());
            }

CodePudding user response:

\Str::lower() actually does nothing for you here since it just returns string 'name', which is then passed as first argument in Builder::where() function.

What you can do it take advantage of Builder::whereRaw() function and do something like following

Category::whereRaw('LOWER(`name`) = ?', \Str::lower(request('name')))->first()

But Eloquent build also works if you do something like (do not use = sign)

Category::whereName(request('name'))->first()

or

Category::where('name', request('name'))->first()
  • Related