Laravel Eloquent - id来命名关系

时间:2014-09-11 23:25:56

标签: php mysql laravel-4 eloquent

我现在在Laravel 4中使用雄辩,我想加入所有两张桌子。我目前的sql是:

SELECT h.project_status_id, s.name FROM project_history as h LEFT JOIN project_status as s ON h.project_status_id = s.project_status_id;

如果它有助于我建立雄辩的关系。

ProjectBaseModel::find($project_id)->history;

我抓取属于特定项目的所有历史记录条目,但 project_history 表中有 project_status_id ,当我在视图中显示它时,我想要将 project_status_id 替换为 project_status 表中的字符串名称

e.g。

表:Project_History

------------------- ---------------
| project_status_id| comment       |
------------------- ---------------
| 2                | Comment 2     |
------------------  ---------------
| 4                | Comment 4     |
------------------  ---------------
| 5                | Comment 5     |
------------------  ---------------

表:Project_Status

------------------ -----------
|project_status_id| name      |
------------------ -----------
| 2               | Status 2  |
------------------ -----------
| 4               | Status 4  |
------------------ -----------
| 5               | Status 5  |
------------------ -----------

所需的表格:

 ----------- -----------
| name      | comment   |
 ----------- -----------
| Status 2  | Comment 2 |
 ----------- -----------
| Status 4  | Comment 4 |
------------------------
| Status 5  | Comment 5 |
------------------------

0 个答案:

没有答案