Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Eloquent: Query the length of field in Laravel

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();
like image 790
JLM Avatar asked May 24 '16 09:05

JLM


People also ask

How do you find the length of a string in Laravel?

You can use laravel helper method Str::length() to find string length or you can use php strlen() method .

How can I count the number of rows in Laravel?

$wordlist = Wordlist::where('id', '<=', $correctedComparisons)->get(); $wordCount = $wordlist->count(); The Count Number Of Rows Laravel Controller was solved using a number of scenarios, as we have seen.

What is Laravel eloquent query?

The Eloquent ORM included with Laravel provides a beautiful, simple ActiveRecord implementation for working with your database. Each database table has a corresponding "Model" which is used to interact with that table. Before getting started, be sure to configure a database connection in config/database. php .

What is DB row in Laravel?

DB::raw() is used to make arbitrary SQL commands which aren't parsed any further by the query builder. They therefore can create a vector for attack via SQL injection. So in my example I guess that the query with DB::raw is faster since laravel doesn't check or validate anything.


1 Answers

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

User::with('Permissons')->whereRaw('LENGTH(name) > 50')->get();
like image 118
vijaykumar Avatar answered Sep 20 '22 06:09

vijaykumar