我构建了一个枢轴表,其中包含我想要关联的表的I。当我拥有特定项的id时,我现在想要获得保存在pivot表中的该项的最新条目。例如:
Table 1: Tickets
Table 2: Status
Table 3: Ticket_Status (Pivot)如果我在枢轴表中添加一个新条目,我将得到如下内容:
枢轴
ticketId,statusId
1, 2
1, 3
2, 1现在我想要获得Id 1枢轴中的最新状态,所以我希望收到statusId 3的机票1,但是我如何在Laravel中这样做呢?
为pivot表创建条目是可行的:
public function attachDispatchStatus($id) {
$this->status()->attach($id);
$this->touch();
}
// set fields on the eloquent object and save to database
// raise event that the incident was created.
public function createDispatch($command) {
// Get BodyContent from POST Request
$this->dispatchReference = $command->dispatchReference;
$this->incidentReference = $command->incidentReference;
// Create new Dispatch
$dispatch = Dispatch::create(array(
'dispatch_reference' => $this->dispatchReference,
'incident_reference' => $this->incidentReference
));
$dispatchStatus = DispatchStatus::where('status', '=', 'processing')->first();
$dispatch->attachDispatchStatus($dispatchStatus->id);
return $this;
}发布于 2015-12-30 15:27:56
在编辑票时,为什么不使用Laravel 5中的updateExistingPivot($roleId, $attributes);呢?
这将解决您的问题,并使您的数据库更轻松:)
查看拉维尔博士以获得有关pivot表的一些示例。
如果您不想这样(因为您希望保持输入的历史意义),我认为您必须在枢轴表中添加一个dateTime字段.然后,按日期点餐,你就会没事的。
https://stackoverflow.com/questions/34532130
复制相似问题