How to make request with substr in eloquent (laravel 4)?

I have this query:

select substr(id,1,4) as id from meteo.a2012 group by substr(id,1,4) 

I just want to take the first 4 numbers in my id string, but I'm trying to make it eloquent, how do I do it?

Thanks.

+4
source share
2 answers

You need to use raw expressions so you can use special functions.

 Model::select(DB::raw('substr(id, 1, 4) as id'))->groupBy(DB::raw('substr(id, 1, 4)'))->get(); 

Where Model is your Eloquent model that you want to run the query on.

+6
source
 $ids = Model::get(['id']); foreach ($ids as $str) { $str->id =substr($str->id,1,4); } return $ids; 
-1
source

Source: https://habr.com/ru/post/1481537/


All Articles