如何将MySQL查询转换为Laravel Eloquent查询?

时间:2015-12-01 05:06:01

标签: php mysql laravel

以下是查询:

SELECT * FROM `users` as m
left join `tutor_segments` as ts on m.id = ts.tutor_id
left join subject_segments as seg on ts.segment_id = seg.id 
left join users_area as ua on ua.user_id=m.id
left join area as a on ua.area_id=a.id
where m.user_type=2 and (seg.segment_name like '%s%'
or a.area_name like '%s%'  or m.area like '%s%') 
group by m.id;

如何使用此部分的and (seg.segment_name like '%s%' or a.area_name like '%s%' or m.area like '%s%')

的查询构建器构建laravel查询

4 个答案:

答案 0 :(得分:1)

试试这个,

$users = DB::table('users')
    ->leftJoin('tutor_segments', 'users.id', '=', 'tutor_segments.tutor_id')
    ->leftJoin('subject_segments', 'tutor_segments.segment_id', '=', 'subject_segments.id')
    ->leftJoin('users_area', 'users_area.user_id', '=', 'users.id')
    ->leftJoin('area', 'user_area.area_id', '=', 'area.id')
    ->where('users.user_type', 2)
    ->where(function($query){
     $query->where('area.area_name', 'LIKE', '%s%')
        ->OrWhere('users.area_name', 'LIKE', '%s%')
        ->OrWhere('subject_segments.segment_name', 'LIKE','%s%');
     })       
    ->groupBy('users.id')
    ->get();

也请阅读doc

答案 1 :(得分:0)

也许是这样的?我根本没有使用laravel,但是根据你如何做的文档。

http://laravel.com/docs/4.2/eloquent

http://laravel.com/docs/5.1/queries

代码

$users = DB::table('users')
    ->leftJoin('tutor_segments', 'users.id', '=', 'tutor_segments.tutor_id')
    ->leftJoin('subject_segments', 'tutor_segments.segment_id', '=', 'subject_segments.id')
    ->leftJoin('users_area', 'users_area.user_id', '=', 'users.id')
    ->leftJoin('area', 'user_area.area_id', '=', 'area.id')
    ->select('users.*')
    ->whereRaw(
        'users.user_type=2 and 
        (
            subject_segments.segment_name like ?
            or 
            area.area_name like ?  or users.area like ?
        )', array('%s%', '%s%', '%s%')
    )
    ->groupBy('users.id')
    ->get();

答案 2 :(得分:0)

DB::table('users')
        ->leftJoin('tutor_segments', 'users.id', '=', 'tutor_segments.tutor_id')
        ->leftJoin('subject_segments', 'subject_segments.segment_id', '=', 'tutor_segments.tutor_id')
        ->leftJoin('users_area', 'users_area.user_id', '=', 'users.id')
        ->leftJoin('area', 'area.id', '=', 'users_area.area_id')
        ->where('users.user_type', 2)
        ->where(function($query){
            $query->where('subject_segments.segment_name', 'LIKE', '%s%')
            ->orWhere('area.area_name', 'like', '%s%')
            ->orWhere('users.area', 'like', '%s%')
           })
        ->groupBy('users.id')
        ->get();

答案 3 :(得分:0)

您可以在laravel whereRaw()

中编写原始查询
ls

@Antony Thompson他需要从users表中选择所有数据,因此他需要使用$users = DB::table('users') ->leftJoin('tutor_segments', 'users.id', '=', 'tutor_segments.tutor_id') ->leftJoin('subject_segments', 'tutor_segments.segment_id', '=', 'subject_segments.id') ->leftJoin('users_area', 'users_area.user_id', '=', 'users.id') ->leftJoin('area', 'user_area.area_id', '=', 'area.id') ->whereRaw("users.user_type=2 and (subject_segments.segment_name like '%s%' or area.area_name like '%s%' or users.area like '%s%')") ->select('users.*') ->groupBy('users.id') ->get(); 而不是->select('users.*'),并且您的查询将生成错误,因为您没有为所选表创建别名。