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.
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.
Model
$ids = Model::get(['id']); foreach ($ids as $str) { $str->id =substr($str->id,1,4); } return $ids;
Source: https://habr.com/ru/post/1481537/More articles:Why do I have JSON.stringify before I .parseJSON get the result from the Google+ API? - jsonDeclaring a description without changes inside a loop initialization statement - c ++phantomjs: unknown modular system - phantomjsSyndicationFeed: How to access content: encoded? - c #How can I manage classes like Twitter Bootstrap "row" and "span (X)" to work with @media print? - cssUpdating AJAX list, getting new items and counting - javascriptShow jQuery onSubmit dialog when processing HTML form - jqueryWhat data type is optimal for a clustered index on a table published using transactional replication? - sqlMethod override passing null object - javaXcode - break in exception but unused characters - debuggingAll Articles