简体   繁体   English

在laravel中进行此查询的最佳方法是什么?

[英]What would be the best way to conduct this query in laravel?

I am working on my first project in laravel and now I perform the search functionality, however, I am doing a query which involves several left join and when searching returns me a bit redundant information. 我正在laravel中从事我的第一个项目,现在我执行搜索功能,但是,我正在执行一个涉及多个左连接的查询,当搜索返回时,我得到了一些多余的信息。

The related tables are these four, and the query is as follows: 相关的表是这四个表,查询如下:

演示

$searchData = ProfessionalDetail::select('u.id as user_id','u.first_name','u.last_name','u.profile_pic','c.name as city_name','professional_details.avg_ratings')
    ->join('users as u' , 'u.id', '=' ,'professional_details.user_id')
    ->join('user_speciality as us' , 'us.user_id', '=' ,'professional_details.user_id')
    ->join('specialities as sp' , 'sp.id', '=' ,'us.speciality_id')
    ->join('cities as c' , 'c.id', '=' ,'professional_details.city_id')
    ->join('users_roles as ur' , 'ur.user_id', '=' ,'professional_details.user_id')
    ->where( function ( $q2 ) use ( $name) {
         $q2->where('u.first_name' , 'like', '%' . $name . '%')
         ->orWhere('u.last_name','like', '%'.$name.'%')
         ->orWhere('u.first_name','like',$name.'%');
         ->orWhere('sp.name','like',$name.'%');
    })
    ->where('ur.role_id' ,'=' , 2)
    ->paginate($num_per_page);

    return $searchData;[![demo][1]][1]

What I tried, is that users are listed, by name or by specialty (it is a doctors page), when ready by specialty everything is going well. 我尝试过的是,按名称或专业列出用户(这是医生页面),按专业列出时,一切进展顺利。 But when I want to list by users, having the left join 'user_speciality' and 'professional_details'. 但是,当我要按用户列出时,请左连接'user_speciality'和'professional_details'。 This returns me the user numerous times. 这使我多次回馈用户。

For example: 例如:

public function testApi(Request $request)
{
    $users = ProfessionalDetail::searchByNameAndSpe("joshi", 10);
    $i = 0;
    if ($users) {
        foreach ($users as $key => $value) {
            $response["result"][] = $value;
            $response["total"] = $i++;
        }

    } else {
        return "error";
    }

    return $response;
}

Returns: 返回:

{"result":[{"user_id":204,"first_name":"Joshi","last_name":"Mohit","profile_pic":"1495436016.jpg","name":"Skin Specialist","city_name":"Pune","avg_ratings":"0.00"},{"user_id":204,"first_name":"Joshi","last_name":"Mohit","profile_pic":"1495436016.jpg","name":"Heart Specialist","city_name":"Pune","avg_ratings":"0.00"},{"user_id":204,"first_name":"Joshi","last_name":"Mohit","profile_pic":"1495436016.jpg","name":"ENT Specialist","city_name":"Pune","avg_ratings":"0.00"},{"user_id":204,"first_name":"Joshi","last_name":"Mohit","profile_pic":"1495436016.jpg","name":"Kidney Specialist","city_name":"Pune","avg_ratings":"0.00"},{"user_id":204,"first_name":"Joshi","last_name":"Mohit","profile_pic":"1495436016.jpg","name":"Eye Specialist","city_name":"Pune","avg_ratings":"0.00"}],"total":4}

Joshi is only registered once, however I know that he will be returned numerous times due to the relation 'many to many' since he owns several specialties. Joshi仅注册一次,但是我知道由于他拥有多个专业,由于“多对多”的关系,他将被无数次遣返。

演示

Now, to get it to search by specialty and the name of the users, the only way I know is to use the left join, I think a way to separate the information would be handling the information of the array in the controller, however II would like to know if this can be resolved in the query, since I do not know much about SQL, if there is any way I would be very grateful as it would save me many steps to debug all the information. 现在,要按专业名称和用户名称进行搜索,我知道的唯一方法是使用左连接,我认为分离信息的一种方法是在控制器中处理数组的信息,但是II我想知道这是否可以在查询中解决,因为我对SQL不太了解,如果有什么办法,我将不胜感激,因为它可以节省许多调试所有信息的步骤。

The methods of each model 每种模型的方法

UserSpeciality table UserSpeciality表

public function users() {

    return $this->belongsToMany('App\User', 'user_speciality', 'speciality_id', 'user_id');
}

User 用户

  public function userSpeciality() {
        return $this->belongsToMany('App\Speciality','user_speciality', 'user_id', 'speciality_id');
}

speciality 专业

public function users() {

    return $this->belongsToMany('App\User', 'user_speciality', 'user_id', 'speciality_id');
}

ProfesionalDetail table 专业详情表

    public function user(){
        return $this->belongsTo("App\User");
    }

Have try once group by user id 尝试一次按用户ID分组

groupBy(user_id) GROUPBY(USER_ID)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM