Beaumont
Beaumont

Reputation: 363

Laravel/PostgreSQL query with non-capitalised letters

I am doing a Laravel CRUD exercise and I have set up a search route in the controller:

public function search($name)
    {
        return Product::where('name', 'like', '%'.$name.'%')->get();
    }

And on the backend psql database, I have an item name exactly as iPhone 11.

When I ran a query on postman, it worked fine if I GET by localhost:8000/api/products/search/iPh. However, if I uncapitalised it as ...search/iph, it would return an empty array.

So my question is, what do I do so I can search with uncapitalised letters while the data stored contains capitalised letters?

Upvotes: 0

Views: 50

Answers (1)

S N Sharma
S N Sharma

Reputation: 1526

For MySQL you can use raw query

Product::whereRaw("UPPER(name) LIKE '%'". strtoupper($name)."'%'")->get(); 

For pgSQL you can use ILIKE for case in-sensitive and LIKE for case sensitive

Product::where('name', 'ILIKE', '%'.$name.'%')->get();

Upvotes: 1

Related Questions