0

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?

1 Answer 1

1

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Good tip! Do you have any recommendation on a cheat sheet for this type of query for mysql and pgsql?
You can check Laravel Doc. There is not such kind of cheat sheet. Only few queries might not be compatible. You will find the solution for them easly.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.