我想得到那些已经申请旅行的客户的电子邮件。
我有三张桌子 trips
, service_requests
, users
以下是我的表格是如何关联的
在Trips模型中
public function requests(){
return $this->hasMany(ServiceRequest::class);
}
在ServiceRequest模型中
public function customer()
{
return $this->belongsTo(User::class, "customer_id");
}
这是我用来获取结果的代码,我得到了我想要的结果,但我只想得到客户的电子邮件地址数组。
$trip_customers = Trip::with('requests.customer')
->whereHas('requests', function($query){
$query->where('status', 2);
})->get()->pluck('requests');
上面的输出是
Array
(
[0] => Array
(
[0] => Array
(
[id] => 1
[user_id] => 2
[customer_id] => 1
[post_ad_id] =>
[trip_id] => 1
[pickup_date] => 2020-06-15
[dropoff_date] => 2020-06-18
[address] => Lane C 20, Gokul Enclave
[contact_no] => 8451515
[weight] => 681
[person] =>
[additional_comments] => eretrt
[status] => 2
[view_status] => 1
[customer] => Array
(
[id] => 1
[name] => Customer
[email] => [email protected]
[mobile_number] => 51551
[status] => 1
)
)
)
)
你可以尝试从Customer端进入,因为你只需要从该表获取信息。
Customer::whereHas('requests', function ($query) {
$query->where('status', 2);
})->pluck('email');
假设Customer和ServiceRequest之间有关系.