Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Laravel Eloquent Builder removes the leading zero from the string that binds

I need to find all results that include this substring (note the leading zero):

$code = '02549';

Nomenclature::where('code', 'like', '%' . $code . '%')->get();

With such a builder, I get the correct request:

select * from `nomenclatures` where `code` like '%02549%'

but if I use a condition in the builder:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Nomenclature::when(true, function($query, $code) {
    $query->where('code', 'like', '%' . $code . '%');
})->get();

then I receive such a request (there is no leading zero):

select * from `nomenclatures` where `code` like '%1%'

Can someone explain what’s going on?

>Solution :

i think your code should be like this:

Nomenclature::when(true, function($query) use ($code) {
    $query->where('code', 'like', '%' . $code . '%');
})->get();
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading