使用Laravel 4中的列表关联值,我该怎么做?

时间:2013-09-03 14:08:54

标签: laravel laravel-4 eloquent

我的数据库结构如下:

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

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

要将用户表中的'jobRole'与选项表相关联,我会做这样的事情:

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

当我将laravel中的关系船设置为'belongsTo'时,它运行的查询是:

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

*看看它如何引用列表!

如何在没有引号的情况下运行同一个查询?

目前在我的模型中,我的关系结构如下:

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

1 个答案:

答案 0 :(得分:1)

我同意'peterm'。但这是你的答案:

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

请参阅http://laravel.com/docs/queries#selects