首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >拉勒维尔雄辩的ManyToMany getLatest

拉勒维尔雄辩的ManyToMany getLatest
EN

Stack Overflow用户
提问于 2015-12-30 15:15:09
回答 1查看 43关注 0票数 1

我构建了一个枢轴表,其中包含我想要关联的表的I。当我拥有特定项的id时,我现在想要获得保存在pivot表中的该项的最新条目。例如:

代码语言:javascript
复制
Table 1: Tickets
Table 2: Status
Table 3: Ticket_Status (Pivot)

如果我在枢轴表中添加一个新条目,我将得到如下内容:

枢轴

ticketId,statusId

代码语言:javascript
复制
1, 2

1, 3

2, 1

现在我想要获得Id 1枢轴中的最新状态,所以我希望收到statusId 3的机票1,但是我如何在Laravel中这样做呢?

为pivot表创建条目是可行的:

代码语言:javascript
复制
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;
}
EN

回答 1

Stack Overflow用户

发布于 2015-12-30 15:27:56

在编辑票时,为什么不使用Laravel 5中的updateExistingPivot($roleId, $attributes);呢?

这将解决您的问题,并使您的数据库更轻松:)

查看拉维尔博士以获得有关pivot表的一些示例。

如果您不想这样(因为您希望保持输入的历史意义),我认为您必须在枢轴表中添加一个dateTime字段.然后,按日期点餐,你就会没事的。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34532130

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档