简体   繁体   中英

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.

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'. 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.

演示

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.

The methods of each model

UserSpeciality table

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

groupBy(user_id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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