【问题标题】:Laravel query builder join not working with selectRawLaravel 查询构建器加入不适用于 selectRaw
【发布时间】:2018-05-21 10:48:07
【问题描述】:

我正在使用 laravel 的查询构建器从我的数据库中获取位置。我需要加入 2 个表,然后按位置查询结果。在我添加连接之前一切正常。

如果我这样做

$locations = DB::table('locations')
->join('charities', 'locations.charity', '=', 'charities.charity')
    ->when($charity, function ($query) use ($charity) {
          return $query->where('charity', '=', $charity);
    })
    ->get();

然后它返回我需要的两个表的内容。如果我这样做了

$locations = DB::table('locations')
            ->select('locations.*')
            ->selectRaw('( 3959 * acos( cos( radians(?) ) *
                               cos( radians( latitude ) )
                               * cos( radians( longitude ) - radians(?)
                               ) + sin( radians(?) ) *
                               sin( radians( latitude ) ) )
                             ) AS distance', [$lat, $lng, $lat])
            ->havingRaw("distance < ?", [$max_distance])
            ->when($charity, function ($query) use ($charity) {
                return $query->where('charity', '=', $charity);
            })
            ->get();

然后我得到位置表的内容并且过滤器工作正常。

我将两者放在一起,它只返回没有错误的位置内容,并且没有加入慈善表。

$locations = DB::table('locations')
            ->join('charities', 'locations.charity', '=', 'charities.charity')
            ->select('locations.*')
            ->selectRaw('( 3959 * acos( cos( radians(?) ) *
                               cos( radians( latitude ) )
                               * cos( radians( longitude ) - radians(?)
                               ) + sin( radians(?) ) *
                               sin( radians( latitude ) ) )
                             ) AS distance', [$lat, $lng, $lat])
            ->havingRaw("distance < ?", [$max_distance])
            ->when($charity, function ($query) use ($charity) {
                return $query->where('charity', '=', $charity);
            })
            ->get();

【问题讨论】:

  • 尝试为每一列指定它来自的表的名称。
  • Charity 表中唯一的信息是慈善机构名称,该名称已经在进行加入的位置和慈善机构徽标。其他一切都来自位置表
  • 抱歉没有正确阅读您的帖子,我已经为每一列指定了表格,它很有魅力!谢谢

标签: mysql laravel laravel-5 laravel-eloquent laravel-query-builder


【解决方案1】:

在 MosCH 评论后,我得到了它的工作,非常简单的修复,以防其他人有这个问题,请参阅下面的代码。你会注意到我在 where 子句中指定了 locations.charity 因为慈善是我加入的,我猜它是冲突的。

$locations = DB::table('locations')
            ->join('charities', 'locations.charity', '=', 'charities.charity')
            ->select('*')
            ->selectRaw('( 3959 * acos( cos( radians(?) ) *
                               cos( radians( latitude ) )
                               * cos( radians( longitude ) - radians(?)
                               ) + sin( radians(?) ) *
                               sin( radians( latitude ) ) )
                             ) AS distance', [$lat, $lng, $lat])
            ->havingRaw("distance < ?", [$max_distance])
            ->when($charity, function ($query) use ($charity) {
                return $query->where('locations.charity', '=', $charity);
            })
            ->get();

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-05-11
    • 2021-09-25
    • 1970-01-01
    • 2018-08-12
    • 2017-06-29
    • 2020-03-19
    • 1970-01-01
    相关资源
    最近更新 更多