Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I use substr in laravel eloquent?

Tags:

I use Laravel 5.3

My laravel eloquent is like this :

$query = User::where('year', '=', (string)$object->year)               
             ->where(\DB::raw('substr(code, 1, 3)'), '=', 511)
             ->get();

I try like that, but it does not work

How can I solve it?

like image 289
samuel toh Avatar asked May 04 '17 02:05

samuel toh


People also ask

What is Substr in Laravel?

The substr helper method is a convenient helper built on top of PHP's mb_substr function (unlike PHP's mb_substr function, substr does not provide a way to change the encoding that is used) that is used to return a portion of a string.

How do I search in Laravel eloquent?

Searching Eloquent models Imagine you need to provide a search for users. Using Eloquent you can perform a search like this: User::query() ->where('name', 'LIKE', "%{$searchTerm}%") ->orWhere('email', 'LIKE', "%{$searchTerm}%") ->get();

What is the use of eloquent in Laravel?

Eloquent is an object relational mapper (ORM) that is included by default within the Laravel framework. An ORM is software that facilitates handling database records by representing data as objects, working as a layer of abstraction on top of the database engine used to store an application's data.

What is eloquent in Laravel with example?

An Eloquent model is just a PHP class, that allows you to do two things: Interact with the database table as a whole. For example: $users = User::all(); gets all users. Represent a single row in the table.


1 Answers

you forgot to put comma after '=' sign. try this .

   $query = User::where('year', '=', (string)$object->year)               
           ->where(\DB::raw('substr(code, 1, 3)'), '=' , 511)
           ->get();
like image 174
PHP Dev Avatar answered Sep 21 '22 10:09

PHP Dev