Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Nosean's avatar

How do I create such a query in DB Query Builder?

How do I create such a query in DB Query Builder?

SELECT * FROM table
WHERE left(Colum, 5) LIKE '12345';

Thanks for Help ?

0 likes
3 replies
a4ashraf's avatar

@nosean

btw why you need to use left

try like this, may this help you

$users = DB::table('table')
                ->where(left(Colum, 5), 'like', '12345%')
                ->get();

// try without left

$users = DB::table('table')
                ->where('column', 'like', '12345%')
                ->get();
Nosean's avatar

these are the values in my column (verweis)

177777 0001
177777 0002
177777 0003
188888 0001
188888 0002

etc.

I would like to read out only the lines with the part string 177777.

How can i do this ?

a4ashraf's avatar

@nosean

here is the solution to get the string after space

// Raw Query
SELECT * FROM table WHERE SUBSTRING_INDEX(column, ' ', -1) like '0001'

// Laravel Query Bulider
$users = DB::table('table')
          ->whereRaw('SUBSTRING_INDEX(column, " ", -1) like ?', [0001])
          get()

Please or to participate in this conversation.