gpt4 book ai didi

php - 如何在 Laravel Eloquent 中将表连接到表中

转载 作者:行者123 更新时间:2023-11-29 15:42:30 25 4
gpt4 key购买 nike

我有一个表,即 tbl_servicetable_service与其他4个表的关系如下

tbl_service_reviewtbl_service_imagestbl_service_packages表服务步骤tbl_用户

我已经将这四个表与Tbl_service模型关系连接起来。现在,我想将 table_user 子加入 tbl_service_review.......基本上我想要审阅者 profile_pic 及其来自数据库的姓名......我已经加入了 tbl_user 添加服务的人......

请帮我解决这个问题......预先感谢您。

function get_service_detail_by_id(Request $request) {
$rule = [
'service_id' => 'required',
];

$validate = Validator::make(request()->all(), $rule);

if ($validate->fails()) {
return response()->json(['data' => ['errors' => $validate->errors()->all()], 'message' => 'validation fail', 'status' => '0'], 406);
}
$userId = Auth::id();

$service_id = $request->input('service_id');

$package = Tbl_service::select(array('tbl_service.*',
DB::raw("IFNULL((select IFNULL(avg(rate),0) from tbl_rating where service_id = tbl_service.service_id group by service_id),0) as avg_rating"),
DB::raw("IFNULL((select COALESCE(count(rate),0) from tbl_rating where service_id = tbl_service.service_id group by service_id),0) as total_rating"),
DB::raw("IFNULL((select COALESCE(count(like_to),0) from tbl_service_favorite where like_to = tbl_service.service_id group by service_id),0) as total_like"),
DB::raw("IFNULL((select CASE WHEN id THEN 1 END from tbl_service_favorite where like_to = tbl_service.service_id and like_by = $userId group by tbl_service.service_id),0) as is_like_by_me"),
))
->with('Images', 'Packages', 'User_data', 'Steps')
->where('service_id', $service_id)
->get();

return response()->json(['data' => $package, 'message' => 'Get List Service Detail Successfully', 'status' => 1], 200);
}

表服务模型

public $timestamps = false;
protected $table = 'tbl_service';

public function Images() {
return $this->hasMany('App\Models\Tbl_service_image', 'service_id', 'service_id');
}

public function Packages() {
return $this->hasMany('App\Models\Tbl_service_package', 'service_id', 'service_id');
}

public function User_data() {
return $this->hasMany('App\User', 'id', 'seller_id');
}

public function Steps() {
return $this->hasMany('App\Models\Tbl_service_step', 'service_id', 'service_id');
}

public function Review() {
return $this->hasMany('App\Models\Tbl_rating', 'service_id', 'service_id');
}

}

我当前的回复是

"data": [
{
"service_id": 2,
"category_id": 2,
"seller_id": 1,
"service_title": "text",
"service_package": 3,
"created_at": "2019-08-06 09:20:45",
"avg_rating": "5.0000",
"total_rating": 2,
"total_like": 0,
"is_like_by_me": 0,
"Review": [
{
"rating_by": 2,
"rate": 5,
"rating_text": "Good",
"created_at": "2019-08-06 10:09:33",
},
{
"rating_by": 2,
"rate": 5,
"rating_text": "Good",
"created_at": "2019-08-06 10:09:33",
}
],

"user_data": [
{
"id": 1,
"role": 0,
"thirdparty_id": null,
"login_by": 0,
"name": "Test User",
"email": "test_user@gmail.com",
"country": "India",
"languages": "English",
"profile_pic": "https://images.unsplash.com/photo-1497316730643-415fac54a2af?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
"is_online": 0,
"last_seen": null,
"description": "User Details",
"password": "$2y$10$qf1ohDCSb0x7HrFDvF6cWe.B1V41r4gjuAhMm0DQFyhM1fy7ieKR2",
"temp_pass": null,
"is_active": 1,
"is_blocked": 0,
"user_lat": "21.12345",
"user_long": "72.12345",
"created_at": "2019-08-02 08:49:48"
}
],

}
],
"message": "Get List Service Detail Successfully",
"status": 1

我的预期回应是

"data": [
{
"service_id": 2,
"category_id": 2,
"seller_id": 1,
"service_title": "Text",
"service_package": 3,
"created_at": "2019-08-06 09:20:45",
"avg_rating": "5.0000",
"total_rating": 2,
"total_like": 0,
"is_like_by_me": 0,
"Review": [
{
"rating_by": 2,
"rate": 5,
"rating_text": "Good",
"created_at": "2019-08-06 10:09:33",
"name": "trst",
"profile_pic": "https://images.askmen.com/1080x540/2016/01/25-021526-facebook_profile_picture_affects_chances_of_getting_hired.jpg"
},
{
"rating_by": 2,
"rate": 5,
"rating_text": "Good",
"created_at": "2019-08-06 10:09:33",
"name": "trst",
"profile_pic": "https://images.askmen.com/1080x540/2016/01/25-021526-facebook_profile_picture_affects_chances_of_getting_hired.jpg"
}
],

"user_data": [
{
"id": 1,
"role": 0,
"thirdparty_id": null,
"login_by": 0,
"name": "Test User",
"email": "test_user@gmail.com",
"country": "India",
"languages": "English",
"profile_pic": "https://images.unsplash.com/photo-1497316730643-415fac54a2af?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&w=1000&q=80",
"is_online": 0,
"last_seen": null,
"description": "User Details",
"password": "$2y$10$qf1ohDCSb0x7HrFDvF6cWe.B1V41r4gjuAhMm0DQFyhM1fy7ieKR2",
"temp_pass": null,
"is_active": 1,
"is_blocked": 0,
"user_lat": "21.12345",
"user_long": "72.12345",
"created_at": "2019-08-02 08:49:48"
}
],

}
],
"message": "Get List Service Detail Successfully",
"status": 1

最佳答案

您期待的是合并关系
Here is the link
只需合并Review关系合并useredata->pluck('profile_pic')和review对象

关于php - 如何在 Laravel Eloquent 中将表连接到表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57459276/

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