samuel toh
samuel toh

Reputation: 7076

How can I use substr in laravel eloquent?

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?

Upvotes: 5

Views: 30942

Answers (2)

Nicholas Seetaram
Nicholas Seetaram

Reputation: 21

Late on this thread but for anyone who is having the same issue with the aforementioned topic i found a solution by concatenating the value. For example :-

return User::whereRaw('SUBSTRING(column, -2,  2) = '.$value)->get();

Hope this helps.

Upvotes: 2

PHP Dev
PHP Dev

Reputation: 503

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();

Upvotes: 5

Related Questions