php – Eloquent – 使用字符串值而不是列标题的join子句

发布时间:2022-04-30 发布网站:脚本宝典
脚本宝典收集整理的这篇文章主要介绍了php – Eloquent – 使用字符串值而不是列标题的join子句脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个关于Eloquent中的join子句的问题,以及你是否可以加入字符串值而不是表列.

我有下面的代码查询嵌套集合,通过表’分类法’加入表’目的地’中的父/子记录.

闭包中的第二个$join语句是导致问题的一个; Eloquent假设这是一个列,当我实际上只想加入t1.parent_tyPE =’Destination’时 – 即,t1.parent_type should =一个字符串值,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->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','d1.id');
        $join->where('t1.parent_type',$slug)
    ->get();

替换你的地方

脚本宝典总结

以上是脚本宝典为你收集整理的php – Eloquent – 使用字符串值而不是列标题的join子句全部内容,希望文章能够帮你解决php – Eloquent – 使用字符串值而不是列标题的join子句所遇到的问题。

如果觉得脚本宝典网站内容还不错,欢迎将脚本宝典推荐好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。