gpt4 book ai didi

php - Eloquent - 使用字符串值而不是列标题连接子句

转载 作者:IT王子 更新时间:2023-10-28 23:54:20 26 4
gpt4 key购买 nike

我有一个关于 Eloquent 中的连接子句的问题,以及是否可以连接字符串值而不是表列。

我有下面的代码,通过表“分类法”查询“目的地”表中连接父/子记录的嵌套集。

闭包中的第二个 $join 语句导致了问题; Eloquent 假设这是一个专栏,而我实际上只想加入 t1.parent_type = 'Destination' - 即 t1.parent_type 应该 = 一个字符串值,目的地

$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->on('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();

是否可以强制 Eloquent 这样做?我尝试用 DB::raw('Destination') 替换 'Destination' 但这也不起作用。

谢谢你。

最佳答案

实现相同目标的另一种最佳方法是:

$result = DB::connection()
->table('destinations AS d1')
->select(array('d1.title AS level1', 'd2.title AS level2'))
->leftJoin('taxonomy AS t1', function($join) {
$join->on('t1.parent_id', '=', 'd1.id');
$join->where('t1.parent_type', '=', 'Destination');
})
->leftJoin('destinations AS d2', 'd2.id', '=', 't1.child_id')
->where('d1.slug', '=', $slug)
->get();

where替换你的on

关于php - Eloquent - 使用字符串值而不是列标题连接子句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24449223/

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