0

I want to check if the inputted data already exists in the database. I've been trying to input data that already exists (but with different case) to test it. Here is my query

        $existing = AmenityType::all()
        ->where('type', $req->type)
        ->first();

but it doesn't return anything even if I did this

->where('lower(type)', strtolower($req->type))

The database collation is 'utf8mb4_unicode_ci'

Thank you.

jane doe
  • 69
  • 1
  • 11

1 Answers1

1

Calling all() already returns you a Collection so the where clause is done on the collection instead on the db.

Try it like this:

$binds = array(strtolower($req->type));

$existing = AmenityType::whereRaw('lower(type) = ?'), $binds)->first();

var_dump($existing);
Mihai Matei
  • 24,166
  • 5
  • 32
  • 50