5

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.

Dan
  • 9,391
  • 5
  • 41
  • 73
Carlos Suñol
  • 2,643
  • 2
  • 15
  • 10

3 Answers3

8

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.

Jason Lewis
  • 18,537
  • 4
  • 61
  • 64
1

G'day

I was able to do it like this in Laravel

 $data = DataModel::selectRaw("SUBSTRING_INDEX(EMAIL_COLUMN, '@', 1) as 'alias'")->get();

As a result, I get the alias name (address) from the mail address@domain.net

-1
$ids = Model::get(['id']);
foreach ($ids as $str)
{
    $str->id =substr($str->id,1,4);
}
return $ids;
Pang
  • 9,564
  • 146
  • 81
  • 122
BCPNAYAK
  • 177
  • 3
  • 4
  • 18