In my code below,
I request for the ids
of selected products (by checking a checkbox).
Now, i find the clients of whom the products belong to. Then i get the name and phone number of the clients.
Now the query string
below is an API to send messages to these clients.
but When i send a message, i am not able to seperate the product numbers for the respective clients.
For instance, if Mark and Bill are the clients, the message should be like
Dear Customer, your products with number #343fr3 has been shipped - For Mark`
Dear Customer, your products with number #22543 has been shipped - For Bill
But what happens with mine is, one clients (Mark or Bill) gets the product number of another in the message.
What am i doing wrong or could it be from how the api is structured?
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.";
}
}
}
}
i would go moreless this way:
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.";
}
}
}
I assume shipment belongs to client
The problem is that the data you are trying to loop through doesn't not have a relationship between them. what you can do is,
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.";
}
Hope this helps.