Chuck
Chuck

Reputation: 908

Relating values using lists in Laravel 4, how do i do this?

My database structure is like this:

User
-id
-jobRole (comma separated list of option ids)

Options
-id (included in the list of ids in jobRole above)
-value

To relate 'jobRole' in the user table to the options table I would have done something like this:

jobRoles = SELECT * FROM Options WHERE id IN(1,2,3,4,5)

When I setup the relation ship in laravel as 'belongsTo', the Query it runs is this:

SELECT * FROM Options WHERE id IN('1,2,3,4,5')

*See how it quotes the list!

How can I have laravel run this same query WITHOUT the quotes?

Currently in my model I have the relationship structured like this:

public function jobRole(){
    return $this->belongsTo('JobRole', 'jobRole');
}

Upvotes: 0

Views: 585

Answers (1)

Rob Gordijn
Rob Gordijn

Reputation: 6511

I agree with 'peterm'. But here is your answer:

$Options = Options::whereIn('id', explode(',', $User->jobRole))->get();

see http://laravel.com/docs/queries#selects

Upvotes: 1

Related Questions