how to make query with substr in eloquent (laravel 4)?

末鹿安然 提交于 2019-12-23 09:49:18

问题


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 first 4 numbers to my id row, but I'm trying to do in eloquent, how I do?

Thanks.


回答1:


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

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

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




回答2:


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


来源:https://stackoverflow.com/questions/16626702/how-to-make-query-with-substr-in-eloquent-laravel-4

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!