在我的下面的代码中,
我要求ids
所选产品(通过勾选复选框)。
现在,我找到了产品所属的客户。然后我会得到客户的姓名和电话号码。
现在,下面的query string
是一个向这些客户端发送邮件的API。
但是当我发送消息时,我无法分离相应客户的产品编号。
例如,如果Mark和Bill是客户,则消息应该是
尊敬的客户,您的产品编号为#343fr3已发货 - 适用于Mark`
尊敬的客户,您的产品编号为#22543已发货 - 适用于比尔
但我的情况是,一个客户(Mark或Bill)在邮件中获得另一个客户的产品编号。
我做错了什么,或者可能是api的结构如何?
public function sendAll(Request $request)
{
$ids = $request->ids;
$split = explode(",",$ids);
$shipment = Shipment::findOrFail($split);
if(request()->ajax()) {
$clients = Client::whereHas('products', function($find_clients)use($split)
{
$find_clients->whereIn('id',$split);
})->get();
foreach($shipment as $check_shipment)
{
if($check_shipment->status == 0 )
{
//do something
}
else
{
$get_client_name = [];
$get_client_phone = [];
foreach($clients as $key => $client)
{
$get_client_name[] = $client->name;
$get_client_phone[] = $client->phone;
}
$query = "?key=something&to=".implode(',',$get_client_phone)."&msg=Dear Customer, your products with number ".$check_shipment->id." has been shipped.";
}
}
}
}
答案 0 :(得分:1)
问题是您尝试循环的数据之间没有关系。你能做的是,
public function sendAll(Request $request)
{
$ids = $request->ids;
$split = explode(",",$ids);
$shipment = Shipment::with('client')->findOrFail($split);
foreach($shipment as $check_shipment)
{
if($check_shipment->status == 0 )
{
//do something
}
else
{
$get_client_name []= $check_shipment->client->name;
$get_client_phone [] = $check_shipment->client->phone_no;
}
}
$query = "?key=something&to=".implode(',',$get_client_phone)."&msg=Dear Customer, your products with number ".$check_shipment->id." has been shipped.";
}
希望这会有所帮助。
答案 1 :(得分:0)
我会这样做更多:
public function sendAll(Request $request)
{
$ids = $request->ids;
$split = explode(",",$ids);
$shipment = Shipment::where('status', '>', 0)->whereIn('id', $split);
if(request()->ajax()) {
foreach($shipment->get() as $check_shipment)
{
$query = "?key=something&to=".implode(',',$check_shipment->client->phone)."&msg=Dear Customer, your products with number ".$check_shipment->id." has been shipped.";
}
}
}
我假设货物属于客户