laravel喜欢查询不起作用?

时间:2017-06-07 09:23:32

标签: mysql laravel

这是我的代码:

 $lists = DB::table('connection_request as cr')
                    ->leftJoin('users as u', function($join)
                         {
                             $join->on('u.id', '=', 'cr.sender_id')->orOn('u.id','=', 'cr.receiver_id');
                         })
                    ->select('cr.id as connection_id','cr.sender_id as sen_id','cr.receiver_id as rec_id','cr.approve_status','u.id','u.user_type','u.user_type_id',DB::raw("IF(u.avatar = '', 'uploads/avatar/default.jpg', u.avatar) as avatar"),'u.name','u.email')
                    ->where(function($query) use ($user_id)
                        {
                        if(!empty($user_id)):
                            $query->Where('cr.receiver_id','=', $user_id);
                        endif;
                        if(!empty($user_id)):
                            $query->orWhere('cr.sender_id','=', $user_id);
                        endif;
                     })
                    ->where(function($query) use ($searchValue)
                        {
                        if(!empty($searchValue)):
                           $query->Where('u.name','like', '%' . $searchValue . '%');
                           $query->orWhere('u.email','like', '%' . $searchValue . '%');
                        endif;
                     })
                    ->where('cr.approve_status','=',1)
                    ->where('u.id','!=',$user_id)
                    ->get();

它提供了像这样的

的mysql查询
select `cr`.`id` as `connection_id`, `cr`.`sender_id` as `sen_id`, `cr`.`receiver_id` as `rec_id`, `cr`.`approve_status`, `u`.`id`, `u`.`user_type`, `u`.`user_type_id`, IF(u.avatar = '', 'uploads/avatar/default.jpg', u.avatar) as avatar, `u`.`name`, `u`.`email` from `connection_request` as `cr` left join `users` as `u` on `u`.`id` = `cr`.`sender_id` or `u`.`id` = `cr`.`receiver_id` where (`cr`.`receiver_id` = 10 or `cr`.`sender_id` = 10) and (`u`.`name` LIKE 'pri' or `u`.`email` LIKE 'pri') and `cr`.`approve_status` = 1 and `u`.`id` != 10

所以,我无法获得结果,因为它之后应该有%符号但是,生成不具有百分比符号的laravel查询

我的下面原始查询工作正常

select `cr`.`id` as `connection_id`, `cr`.`sender_id` as `sen_id`, `cr`.`receiver_id` as `rec_id`, `cr`.`approve_status`, `u`.`id`, `u`.`user_type`, `u`.`user_type_id`, IF(u.avatar = '', 'uploads/avatar/default.jpg', u.avatar) as avatar, `u`.`name`, `u`.`email` from `connection_request` as `cr` left join `users` as `u` on `u`.`id` = `cr`.`sender_id` or `u`.`id` = `cr`.`receiver_id` where (`cr`.`receiver_id` = 10 or `cr`.`sender_id` = 10) and (`u`.`name` LIKE '%pri%' or `u`.`email` LIKE '%pri%') and `cr`.`approve_status` = 1 and `u`.`id` != 10

请检查并帮助我们,

由于

2 个答案:

答案 0 :(得分:3)

就个人而言,我使用它并且效果相当好......

$query->where('u.name','LIKE', "%{$searchValue}%")->get();

答案 1 :(得分:2)

你的情况也可以像这样...... 它会工作..

->where(function($query) use ($searchValue)
                       {
                       if(!empty($searchValue)):
                       $query->Where('u.name','like', DB::raw("'%$searchValue%'"));
                       $query->orWhere('u.email','like', DB::raw("'%$searchValue%'"));
                       endif;
                    })