Mindru Ion
Mindru Ion

Reputation: 373

how to search by letter in column in database (laravel)

I want to find by letter customer's first_name on my 'customers' table.

And the only thing that I get is an empty array.

For instance, I put in the parameter 'q' value 'E' to get customer Elena from my database by I get an only empty array.

I use the following code to get first_name : $search = Input::get('q');

    if($search)
    {
        $customers = Customer::all()->where('full_name', 'LIKE', "%{$search}%");
        return ($customers);
    }

Can someone help me?

Upvotes: 0

Views: 999

Answers (2)

schutte
schutte

Reputation: 2166

Try this

$customers = Customer::where('full_name', 'LIKE', "%{$search}%")->get();

Laravel Eloquent

Upvotes: 0

Elias Soares
Elias Soares

Reputation: 10264

Your query don't work because you are calling the all() method before the where(). That's actually not wrong, but it have different behavior.

When you call all(), it actually does the SQL query. After that, any chained methods are being called into a Eloquent Collection class, and it also have a where method, but that's simpler since it runs on PHO instead of running on SQL.

Since the collection's where() method doesn't support LIKE operator, it's probably searching for a value that is exactly %E%.

Hope it can help you understanding why your query doesn't work as expected.

Upvotes: 3

Related Questions