【问题标题】:Using AS Keyword in Laravel Eloquent Join Query In Main Model在主模型的 Laravel Eloquent 连接查询中使用 AS 关键字
【发布时间】:2018-12-15 20:20:12
【问题描述】:

我想像这样在 Laravel Eloquent 模型中使用 AS 关键字:

这是我的示例查询:

User::find(1)
    ->select("name", "age")
    ->join("profile as p", "p.user_id", "=", "users.id")
    ->orderBy("name", "asc")
    ->get();

在与主表/模型的联接中预期使用 u.id 而不是 users.id

->join("profile as p", "p.user_id", "=", "u.id")

有没有这样的功能,可以吗?

【问题讨论】:

  • @Fawzan 我看到了,这是一个很老的问题(5 年前),与我的问题无关。我的问题是关于在主表/模型上使用 as 关键字。

标签: php laravel laravel-5 eloquent


【解决方案1】:

像这样在 eloquent 中使用from 方法

User::from('users as u')->find(1)
    ->select("name", "age")
    ->join("profile as p", "p.user_id", "=", "u.id")
    ->orderBy("name", "asc")
    ->get();

看这个查询雄辩的方法here

【讨论】:

  • 我从方法中知道这一点,但我认为它不适用于 Eloquent,谢谢!
猜你喜欢
  • 1970-01-01
  • 2015-08-19
  • 2012-12-28
  • 2013-05-25
  • 1970-01-01
  • 2018-12-25
  • 2016-06-21
  • 2021-08-27
  • 1970-01-01
相关资源
最近更新 更多