php – laravel mysql查询多个where orwhere和inner join

前端之家收集整理的这篇文章主要介绍了php – laravel mysql查询多个where orwhere和inner join前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在Laravel中准备以下 MySQL查询

这是我使用的代码没有成功:

$user_list = DB::table('users')
->where('users.user_id','=',$clientId)
->where('users.firstname','LIKE','%'.$_POST['name'].'%')
->orWhere('users.lastname','%'.$_POST['name'].'%')
->orWhere('users.email','%'.$_POST['name'].'%')
->join('users_roles','users.id','users_roles.user_id')                        
->where('users_roles.role_id',Role::USER_PARTICIPANT)
->get();

条件必须是:

> users.user_id == $clientId
> users.firstname ==’%’.$_ POST [‘name’].’%’OR users.lastname ==’%’.$_ POST [‘name’].’%’OR users.email ==’% ”.$_ POST [ ‘名’. ‘%’
>当users_roles.role_id == Role :: USER_PARTICIPANT时,users_roles和用户跨越的用户之间的内部联接.id == users_roles.user_id

好吧看起来你的问题是嵌套在哪里

试试这个:

$name = $_POST['name']; // I'd recommend you use Input::get('name') instead...

$user_list = DB::table('users')
    ->where('users.user_id',$clientId)
    ->where(function($query) use ($name){
        $query->where('users.firstname','%'.$name.'%');
        $query->orWhere('users.lastname','%'.$name.'%');
        $query->orWhere('users.email','%'.$name.'%');
    })
    ->join('users_roles','users_roles.user_id')
    ->where('users_roles.role_id',Role::USER_PARTICIPANT)
    ->get();
原文链接:https://www.f2er.com/laravel/135307.html

猜你在找的Laravel相关文章