gpt4 book ai didi

php - 在 Laravel 中执行此查询的最佳方式是什么?

转载 作者:行者123 更新时间:2023-11-29 18:53:16 25 4
gpt4 key购买 nike

我正在 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}

Joshi只注册了一次,但我知道他会因为“多对多”的关系而被返回多次,因为他拥有多个专业。

demo

现在,要让它按专业和用户名进行搜索,我知道的唯一方法是使用左连接,我认为分离信息的一种方法是处理 Controller 中的数组信息,但是我想知道是否可以在查询中解决这个问题,因为我对 SQL 不太了解,如果有任何方法我将非常感激,因为它会节省我调试所有信息的许多步骤。

各个模型的方法

用户专业表

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

专业详情表

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

最佳答案

尝试过一次按用户 ID 分组

groupBy(user_id)

关于php - 在 Laravel 中执行此查询的最佳方式是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285550/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com