2017-05-31 180 views
0

我正在研究laravel中的第一個項目,現在我執行搜索功能,但是,我正在做一個涉及多個左連接的查詢,並且在搜索時會返回一些冗餘信息。在laravel中執行此查詢的最佳方式是什麼?

相關的表是這四和查詢如下:

demo

$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] 

我試了一下,是用戶都列出來,通過名稱或專業(這是一個醫生頁面),當專業準備好時,一切進展順利。但是當我想按用戶列出時,有左連接'user_speciality'和'professional_details'。這使用戶無數次返回給我。

例如:

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; 
} 

返回:

{"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} 

喬希只能註冊一次,但是我知道他會因關係「多對多」,因爲他返回了無數次擁有幾個專業。現在

demo

,要得到它的專業和用戶的名稱進行搜索,我所知道的唯一的辦法就是使用左加入,我想辦法給信息分離將被處理的信息但是II想知道這是否可以在查詢中解決,因爲我對SQL不太瞭解,如果有什麼辦法,我會非常感激,因爲它會爲我節省很多調試步驟所有的信息。

每個模型的方法

UserSpeciality表

public function users() { 

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

用戶

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

專業

public function users() { 

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

ProfesionalDetail表

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

你有模型建立這些表? –

+0

如果和每一個都有其相應的關係方法, 我修改了帖子並添加了他們 –

回答

0

有用戶ID嘗試一次組

GROUPBY(USER_ID)

+0

感謝的人,這是工作 –

相關問題