jrsalunga
jrsalunga

Reputation: 419

Laravel 4: Get specific field using Eloquent ORM Model

I'm very new to Laravel, having a database like this:

code  |     descriptor    |    id
_______________________________________
frd     Ford                1
chv     Chevrolet           2
fer     Ferrari             3

Model:

class Car extends Eloquent {}

How do I query using the code to get the descriptor?

Like this: SELECT descriptor FROM car WHERE code = 'fer'

Upvotes: 0

Views: 1984

Answers (1)

Tarik
Tarik

Reputation: 2261

As stated in laravel's documentation you can use where with models

$cars= Car::where('code', '=', 'fer')->get();

And second paramater is not obligatory if it is '='

$cars= Car::where('code', 'fer')->get();

Edit:

If you just want an array of a single column you can use this

$cars= DB::table('cars')->where('code', 'fer')->lists('id');

Upvotes: 1

Related Questions