DMS-KH
DMS-KH

Reputation: 2787

how to select specify with the first character in a column in Laravel?

I want to select all the rows with contain with first number 1 from those rows as below data in image

$data = Modales::where('type', 5)->where('code', 'like', '%1%')->get();

enter image description here

Upvotes: 0

Views: 3651

Answers (2)

Marcus Olsson
Marcus Olsson

Reputation: 2527

Your question is rather oddly asked, but I am gathering from your comments that you want to query the table where a fields contains (and first character) matches your query, and then you only want to retrieve the first character of a specific field.

So, for instance:

Table Users

id | type     | name
1  | admin    | Patrick
2  | standard | Lucas
3  | standard | Anna

Then you would write your Eloquent query:

$users = User::where('type', 'LIKE', $char . '%')
    ->selectRaw('*, LEFT (name, 1) as first_char')
    ->get();

Then if $char = "s", then first_char would contain the first character of Lucas and Anna.

Upvotes: 2

Virb
Virb

Reputation: 1578

You have to use just like this:

->where($field, 'LIKE', $letter.'%');

Upvotes: 3

Related Questions