Shivang Trivedi
Shivang Trivedi

Reputation: 49

get duplicates from whereIn - Laravel

I am currently trying to get the data from X table having columns id and name. I am using Laravel for the project and into it whereIn to get the multiple data from X table. Its working perfect and I am getting expected response when I am sending request data as (1,2,3,4) . But when I am sending request as (1,2,3,4,2) it is providing response for id 1,2,3,4 but it not retrieving the duplicate data again that is for id 2. Below is my query

 X::whereIn('id',$request->XIds)->get()->toArray();

Suppose $request->XIds is [1,2,3,4,2].. As 2 is repeating. My question is how to get duplicate data through the query so that when I send request having duplicate entries I can get response having that duplicate data.

Upvotes: 1

Views: 2112

Answers (5)

xxstevenxo
xxstevenxo

Reputation: 669

Expanding on @apokryfos solution. In order to return a single-dimensional collection of objects, you should use ->first().

$data = X::whereIn('id',$request->XIds)->get()->getDictionary();

return collect($request->XIds)->map(function ($id) use ($data) {
      return $data->get($id)->first(); 
});  

$data output:
enter image description here

mapped $data ouput:
enter image description here

Upvotes: 1

Dhruv Koshiyar
Dhruv Koshiyar

Reputation: 167

Here you go.

$result = X::whereIn('id', $request->XIds)->get();
$ids = $result->pluck('id')->toArray();
$resultInArray = $result->toArray();
$finalResult = collect($request->XIds)->map(function ($id) use($ids, $resultInArray) {
        return $resultInArray[array_search($id, array_values($ids))];
});

Upvotes: 0

apokryfos
apokryfos

Reputation: 40653

Well, you can project the data correctly if you want:

$data = X::whereIn('id',$request->XIds)->get()->getDictionary();


return collect($request->XIds)->map(function ($id) use ($data) {
      return $data->get($id)->toArray(); 
})->all();  

However, I'd suggest you try and review your logic a bit because there's no objective advantage of sending the same data multiple times.

Upvotes: 2

Mihai Matei
Mihai Matei

Reputation: 24276

You have to do it in PHP after you get the data from the db.

$collection = X::whereIn('id', $request->XIds)->get()->keyBy('id');

$counts = array_count_values($request->XIds);

foreach ($counts as $id => $count) {
    if ($count === 1) {
         continue;
    }
    for ($i = 0; $i < $count - 1; $i++) {
        $collection->push($collection->get($id));
    }
}

$yourFinalArray = $collection->values()->toArray();

Upvotes: 0

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

whereIn('id',array(1,2,3,4,2)) will return data that matches id field value in given array , so it will not return duplicate value , basically it checks that id is inarray or not ? if is inarray then select that data .

if you need duplicate data , you have to manualy write code like this :

$my_data=[];

 foreach($request->XIds as $id){
     $xmodel=X::where('id',$id)->first();
     if($xmodel){
       array_push($my_data,$xmodel);
     }
 }

return response()->json($my_data,200);

Upvotes: -2

Related Questions