Home » Php » Eloquent: Query the length of field in Laravel

Eloquent: Query the length of field in Laravel

Posted by: admin November 29, 2017 Leave a comment

Questions:

I want to do something like that in Laravel (valid sqlite query):

select * from 'tbUsers' where  length(name)>50;

I tried

User::with('Permissons')->where('LENGTH(name)','>','50')->get();

But it seems not to work…

note: other queries works without problem:

User::with('Permissons')->where('active,'=','1')->get();
Answers:

Try this
whereRaw( string $sql, array $bindings = array(), string $boolean = 'and')

User::with('Permissons')->whereRaw('LENGTH(name) > 50')->get();

Questions:
Answers:

Use whereRaw:

User::with('Permissons')->whereRaw('LENGTH(name) > ?', [50])->get();

Leave a Reply

Your email address will not be published. Required fields are marked *