连接查询结果“在null上调用成员函数leftJoin()”

时间:2017-09-28 05:56:49

标签: laravel join controller query-builder

我正试图在laravel上从我的控制器运行一个连接查询,但我得到“在null上调用一个成员函数leftJoin()”

public function tracking_groupindividual(Request $request)
{
    $user_id = $request->user_id;
    $group_id = $request->group_id;

    $get_group_individual_details = DB::table('group_usermapping as gu')
        ->select(
           'um.user_id', 
           'um.user_name', 
           'um.image_path', 
           'um.mobile_number', 
           'ul.latitude', 
           'ul.longitude', 
           'um.isnearby_on', 
           'gu.isadmin', 
           'um.profile_status', 
           'ul.timedout'
        )
        ->join('user_master as um','gu.user_id','=','um.user_id')
        ->join('groupusers_location as ul','ul.user_id','=','um.user_id','ul.group_id','=','gu.group_id')
        ->where(function ($query) use ($group_id) {
            $query->where('gu.group_id', '=', $group_id)
                ->where('gu.isactive','=','1')->where('um.isactive','=','1');
        })->get();

        return $get_group_individual_details;
    }

2 个答案:

答案 0 :(得分:0)

我在第二次加入时发现问题并修复了它

public function tracking_groupindividual(Request $request)
{
    $user_id = $request->user_id;
    $group_id = $request->group_id;

    $get_group_individual_details = DB::table('group_usermapping as gu')
        ->select(
           'um.user_id', 
           'um.user_name', 
           'um.image_path', 
           'um.mobile_number', 
           'ul.latitude', 
           'ul.longitude', 
           'um.isnearby_on', 
           'gu.isadmin', 
           'um.profile_status', 
           'ul.timedout'
        )
        ->join('user_master as um','gu.user_id','=','um.user_id')
        ->join('groupusers_location as ul', function ($join) {
            $join->on('ul.user_id','=','um.user_id')
                ->where('ul.group_id','=','gu.group_id');
        })
        ->where(function ($query) use ($group_id) {
            $query->where('gu.group_id', '=', $group_id)
                ->where('gu.isactive','=','1')->where('um.isactive','=','1');
        })->get();

        return $get_group_individual_details;
    }

答案 1 :(得分:0)

尝试将DB :: raw()与别名一起使用!!!

$get_group_individual_details = DB::table(DB::raw('group_usermapping as gu'))
                                ->join(DB::raw('user_master as um'),DB::raw('gu.user_id'),'=',DB::raw('um.user_id'))
                                ->join(DB::raw('groupusers_location as ul'),DB::raw('ul.user_id'),'=',DB::raw('um.user_id'),DB::raw('ul.group_id'),'=',DB::raw('gu.group_id'))
                                ->select(DB::raw('um.user_id'),DB::raw('um.user_name'),DB::raw('um.image_path'),DB::raw('um.mobile_number'),DB::raw('ul.latitude'),DB::raw('ul.longitude'),DB::raw('um.isnearby_on'),DB::raw('gu.isadmin'),DB::raw('um.profile_status'),DB::raw('ul.timedout'))
                                ->where(function ($query) use ($group_id) {
                                        $query->where(DB::raw('gu.group_id'), '=', $group_id)->where(DB::raw('gu.isactive'),'=','1')->where(DB::raw('um.isactive'),'=','1');
                                         })->get();

return $get_group_individual_details;

并将使用Illuminate \ Support \ Facades \ DB; 替换为使用数据库;