Laravel 5.3加入场地价值

时间:2016-10-17 07:50:33

标签: laravel laravel-5.2

此Laravel模型查询

PostModel::select('posts.*', 'users.name', 'users.user_fullname', 'users.user_gender', 'users.user_picture', 'users.user_picture_id', 'users.user_cover','users.user_cover_id', 'users.user_verified', 'users.user_pinned_post', 'pages.*', 'groups.group_name', 'groups.group_picture_id', 'groups.group_cover_id', 'groups.group_title', 'groups.group_admin', 'groups.group_pinned_post')
                ->leftjoin('users',function($flj){
                    $flj->on('posts.user_id', '=' ,'users.id')
                    ->where('posts.user_type','=','user');
                })
               ->leftjoin('pages',function ($slj){
                   $slj->on('posts.user_id', '=', 'pages.page_id')
                       ->where('posts.user_type','=','page');
               })
                ->leftjoin('groups',function ($tlj){
                    $tlj->on('posts.in_group','=',1)
                        ->whereColumn('posts.group_id', '=', 'groups.group_id');
                })
            ->whereRaw('NOT (users.name <=> NULL AND pages.page_name <=> NULL)')
            ->where('posts.post_id','=',$id)->get();

当查询运行然后给我这个错误

SQLSTATE[42S22]: Column not found: 1054 Unknown column '1' in 'on clause' (SQL: select `posts`.*, `users`.`name`, `users`.`user_fullname`, `users`.`user_gender`, `users`.`user_picture`, `users`.`user_picture_id`, `users`.`user_cover`, `users`.`user_cover_id`, `users`.`user_verified`, `users`.`user_pinned_post`, `pages`.*, `groups`.`group_name`, `groups`.`group_picture_id`, `groups`.`group_cover_id`, `groups`.`group_title`, `groups`.`group_admin`, `groups`.`group_pinned_post` from `posts` left join `users` on `posts`.`user_id` = `users`.`id` and `posts`.`user_type` = user left join `pages` on `posts`.`user_id` = `pages`.`page_id` and `posts`.`user_type` = page left join `groups` on `posts`.`in_group` = `1` and `posts`.`group_id` = `groups`.`group_id` where NOT (users.name <=> NULL AND pages.page_name <=> NULL) and `posts`.`post_id` = 360)

如果我直接在数据库上运行此查询没有错误 查询是

select `posts`.*, `users`.`name`, `users`.`user_fullname`, `users`.`user_gender`, `users`.`user_picture`, `users`.`user_picture_id`, `users`.`user_cover`, `users`.`user_cover_id`, `users`.`user_verified`, `users`.`user_pinned_post`, `pages`.*, `groups`.`group_name`, `groups`.`group_picture_id`, `groups`.`group_cover_id`, `groups`.`group_title`, `groups`.`group_admin`, `groups`.`group_pinned_post` from `posts` left join `users` on `posts`.`user_id` = `users`.`id` and `posts`.`user_type` = 'user' left join `pages` on `posts`.`user_id` = `pages`.`page_id` and `posts`.`user_type` = 'page' left join `groups` on `posts`.`in_group` = 1 and `posts`.`group_id` = `groups`.`group_id` where NOT (users.name <=> NULL AND pages.page_name <=> NULL) and `posts`.`post_id` = 360

如何使用不在laravel的地方?

谢谢

1 个答案:

答案 0 :(得分:0)

您可以替换

->whereRaw('NOT (users.name <=> NULL AND pages.page_name <=> NULL)')

->whereNotNull('users.name')
->whereNotNull('pages.page_name')

编辑: 替换此

->leftjoin('groups',function ($tlj){
                $tlj->on('posts.in_group','=',1)
                    ->whereColumn('posts.group_id', '=', 'groups.group_id');

->leftjoin('groups',function ($tlj){
                $tlj->on('posts.group_id','=', 'groups.group_id');

并将in_group放在->whereNotNull()

之前
->where('posts.in_group', '=', 1)