mask man
mask man

Reputation: 443

Using aliases with eloquent

When I tried using alias using select with eloquent, it has given me weird results

my code:

$caseStudy = CaseStudy::find($id)->select('title_case as title','description_case as description')->get();

It returned collection. My question is to find model and change the column names. How to do it?

Upvotes: 0

Views: 264

Answers (2)

Imtiaz Pabel
Imtiaz Pabel

Reputation: 5445

try using this

$caseStudy = CaseStudy::select(DB::raw('title_case as title','description_case as description'))->find($id)->get();

Upvotes: 0

Needpoule
Needpoule

Reputation: 4576

The find function can take an array as a second parameter. In this array you can list the fields you need.

$caseStudy = CaseStudy::find($id, ['title_case as title','description_case as description']);

This way you should be able to access your aliases:

$caseStudy->title

Instead of:

$caseStudy->title_case

Upvotes: 1

Related Questions