雄辩 - 用字符串值而不是列标题连接子句

2022-08-30 17:44:03

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

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

关闭中的第二个语句是导致问题的声明;Eloquent假设这是一列,而我实际上只想加入 - 即,应该=一个字符串值,.$joint1.parent_type = 'Destination't1.parent_typeDestination

$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这样做?我尝试过替换为,但这也不起作用。'Destination'DB::raw('Destination')

谢谢你。


答案 1

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

$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();

将您的替换为onwhere


答案 2

尝试使用DB::raw("'Destination'")


推荐