Laravel 5.5中的复杂查询生成器?

时间:2017-12-13 15:01:54

标签: php laravel

您好我有一个原始查询,看起来像这样,必须在Laravel 5.5中的查询构建器中进行转换而不使用eloquent并且需要进行分页

select lt.country_name, u.user_id, u.real_name from users u
join lang_table lt on lt.language_id = u.country
where not exists (select fr2 from friends where fr1 = 1 and fr2 = u.user_id and status = 1) and not exists(select fr1 from friends where fr2 = 1 and fr1 = u.user_id and status = 1) 
and u.user_id <> 1
and u.country = 1

尝试下面这样的很多变种,但每次都给我一个错误 如果有人可以帮助我?

$data = DB::table('users')
               ->join('lang_table','lang_table.language_id', '=' ,'users.country')
             ->select('lang_table.country','users.user_id','users.real_name')
               ->select(DB::raw(where not exists (select fr2 from friends where fr1 = 1 and fr2 = u.user_id and status = 1) and not exists(select fr1 from friends where fr2 = 1 and fr1 = u.user_id and status = 1) 
and u.user_id <> 1
and u.country = 1))
               ->paginate(25);

2 个答案:

答案 0 :(得分:0)

尝试在config \ database.php中将strict更改为false

'mysql' => [
            'driver' => 'mysql',
            'host' => env('DB_HOST', '127.0.0.1'),
            'port' => env('DB_PORT', '3306'),
            'database' => env('DB_DATABASE', ''),
            'username' => env('DB_USERNAME', ''),
            'password' => env('DB_PASSWORD', ''),
            'unix_socket' => env('DB_SOCKET', ''),
            'charset' => 'utf8mb4',
            'collation' => 'utf8mb4_unicode_ci',
            'prefix' => '',
            'strict' => false,
            'engine' => null,
        ],

答案 1 :(得分:0)

我设法写了这个请求,现在给了我以下错误

类型错误:传递给Illuminate \ Database \ Connection的参数1 ::连接:: prepareBindings()必须是数组类型,给定null,在C:\ xampp \ htdocs \ findfriends \ vendor \ laravel \ framework \ src \ Ill中调用第665行的\ uminate \ Database \ Con nection.php

DB::select(`lt.country_name`,`u.user_id`,`u.real_name`)
        ->from(`users as u`)
        ->join(`lang_table as lt`, function($join) {
            $join->on(`lt.language_id`, `=`, `u.country`);
            })
        ->whereRaw(`not exists`, [], `( select fr2 from friends where fr1 = 1 and fr2 = u.user_id and status = 1 )`)
        ->whereRaw(`not exists ( select fr1 from friends where fr2 = 1 and fr1 = u.user_id and status = 1 )`, [], `and`)
        ->where(`u.user_id`, `<>`, 1)
        ->where(`u.country`, `=`, 1)
        ->paginate(25);