gpt4 book ai didi

php - Laravel 4.x 在执行联接时使用 Eloquent 给表起别名

转载 作者:行者123 更新时间:2023-11-29 07:54:36 26 4
gpt4 key购买 nike

我知道在我的模型中我可以像这样为我的表添加别名:

protected $table = 'territories_sign_in_out AS tsio';

这样我仍然可以使用 Eloquent 来构建集合,但是当我需要在像这样的联接中使用表时该怎么办:

Territories::select('territories.id', 'tsio.signed_out')
->join('territories_sign_in_out', function($join) use ($id)
{
//
})
->get();

现在我收到一个错误:

SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error 
in your SQL syntax; check the manual that corresponds to your MySQL server
version for the right syntax to use near 'as `tsio`.`publisher_id` = ? order
by `territory_id` asc, `signed_in` asc' at line 1 (SQL: select * from
`territories_sign_in_out` as `tsio` where `territories_sign_in_out` as
`tsio`.`publisher_id` = 59 order by `territory_id` asc, `signed_in` asc)

我是否只使用 Fluent 就陷入困境?

DB::table('territories_sign_in_out AS tsio')...

我还希望能够使用 Eloquent 构建语句,如下所示:

TerritoriesSignInOut::select('t.id', 't.label', 'tsio.signed_out', 'tsio.publisher_id')
->join('territories AS t', 'tsio.territory_id', '=', 't.id')
->whereNull('tsio.signed_in')
->orderBy('t.label', 'ASC')
->get();

我只能通过添加 as tsio 来做到这一点,就像我在这个问题前面提到的那样。

澄清表格地区 = 模型地区。表territories_sign_in_out = 模型TerritoriesSignInOut

最佳答案

您需要给它起别名:

Territories::select('territories.id', 'tsio.signed_out')
->join('territories_sign_in_out as tsio', function($join) use ($id) {
//...
})
->get();

为了更清楚,我再次重写代码:

Territories::join('territories_sign_in_out as tsio', function($join) use ($id) {
//...
})
->select('territories.id', 'tsio.signed_out')
->get();

关于php - Laravel 4.x 在执行联接时使用 Eloquent 给表起别名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25498073/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com