Laravel Eloquent从二级表中获取数据

时间:2016-05-13 06:25:46

标签: laravel eloquent

我有表:operationsremarksadjustments

operations

| id  | user_id  |  operation   |  start_date |  end_date   |
|  1  |     2    |  opeartion1  |    1/1/16   |   5/5/16    |
|  2  |     2    |  opeartion2  |    1/1/16   |   5/5/16    |

remarks

|  id  |  user_id  |  operation_id  |    remarks    |
|  10  |     4     |       1        |  remarks1 new | 
|  13  |     4     |       2        |  remarks22new |

adjustments

| id | remarks_id | user_id |         before         |             after            |      created_at     |      updated_at     |
|:--:|:----------:|:-------:|:----------------------:|:----------------------------:|:-------------------:|:-------------------:|
|  1 |     10     |    6    | {"remarks":"remarks1"} | | {"remarks":"remarks1 new"} | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |
|  2 |     13     |    7    | {"remarks":"remarks1"} | | {"remarks":"remarks1 new"} | 0000-00-00 00:00:00 | 0000-00-00 00:00:00 |

我想在adjustments控制器的operation_id方法中从Operation表中获取特定show()的数据:

$history = Adjustment::with('user','remark')->get();

从这里我得到adjustments表中的所有行,如下所示:

Jerry Nienow 2016-05-12 07:00:24 {"remarks":"remarks1"} {"remarks":"remarks1 new"}
Jerry Nienow 2016-05-12 07:01:09 {"remarks":"remarks22"} {"remarks":"remarks22new"}

但我想要一个特定的operation_id这样:

Jerry Nienow 2016-05-12 07:00:24 {"remarks":"remarks1"} {"remarks":"remarks1 new"}

1 个答案:

答案 0 :(得分:1)

只需加入remarks表,然后使用此$operationId

获取结果
Adjustment::join('remarks', 'remarks.id','=', 'adjusments.remarks_id')->where('remarks.operation_id', '=', $operationId)->get();

您应首先设置$operationId