【问题标题】:Laravel Eloquent on fixtures club dataLaravel Eloquent 关于赛程俱乐部数据
【发布时间】:2018-05-24 15:46:58
【问题描述】:

我正在尝试返回与团队相关联的灯具列表。赛程表中的主队字段引用了球队表上的 id,客队字段也是如此:

teams
-------
id (int, primary)
name (varchar[255])

fixtures
-------
id (int, primary)
hometeam (int - references `id` on `teams`)
awayteam (int - references `id` on `teams`)
date (datetime)

我有一个单独的夹具和团队模型,目前我只能在夹具表中返回团队 ID,而不是从团队表中返回与其关联的名称。我相信这是因为魔术模型会寻找与字段名称相同的名称,即球队而不是主队或客队。

无论如何我可以让它与 eloquent 一起使用还是需要一个老式的连接?

提前致谢

【问题讨论】:

  • 你能发布模型吗?

标签: laravel-5 eloquent


【解决方案1】:

首先,Eloquent 使用结构化名称搜索键。如果您的外键不同或父模型键不是id,您可以定义相关字段。

参考:One To Many (Inverse)

/**
 * Get the team that owns the fixture.
 */
public function team()
{
    // hometeam: foreign_key
    // id: parent model primary key
    return $this->belongsTo('App\Team', 'hometeam', 'id');
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-04-19
    • 1970-01-01
    • 1970-01-01
    • 2012-11-23
    • 1970-01-01
    • 2021-08-05
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多