XamarinDevil
XamarinDevil

Reputation: 891

Retrieving data from database using multiple ids - Laravel

I have list of clients in my database with with unique ids. I am trying to retrieve these customers using these unique ids like this .

I want to get each clients name and phone and so i can process a message to them respectively. But with my query, i am only getting only one client and not all of my clients.

PS: when i return $explode, i am able to get all the ids i have selected.

  public function getCustomers(Request $request)
     {
        $ids = $request->ids; 

        $explode = explode(",",$ids);

        if(request()->ajax())
        {


        $clients = Client::whereHas('product', function($find_clients)use($explode)
            {
                 $find_clients->where('id',$explode);

            })->get(); 

           $get_customer_name = [];
           $get_customer_phone = [];

            foreach($clients as $key => $client)
            {
                $get_customer_name[] = $client->name;
                $get_customer_phone [] = $client->phone;

                return ['success' => $explode];                                
            }            
           }
}

SMS query

$query = "?key=$api_keyto=$implode(',',$$get_customer_phone)&msg=Dear ".$implode(',',$$get_customer_name)."Thank you";

Upvotes: 0

Views: 69

Answers (2)

Tobias
Tobias

Reputation: 65

To find clients with an array of ids, try using whereIn instead of where.

Like so:

$find_clients->whereIn('id',$explode);

Upvotes: 1

Laraleg
Laraleg

Reputation: 527

Try using whereIn instead of where

$find_clients->whereIn('id', $explode);

Upvotes: 0

Related Questions