Get default response if it does not exist in Laravel’s Wherein

Asked

Viewed 30 times

0

I’m searching a series of items in a DB using Laravel through the function whereIn().

When returning, it presents a Collection with the information found, the problem happens when it does not find an item, it does not present anything related to this item (obviously, because the item does not exist in DB).

Is there any way, in Laravel, to put a standard answer in this Collection being built? Follow example.

How it’s being done:

$item_ids = [1, 2, 4] // item id 4 does not exist, in this case.

$items = Item::whereIn('item_id', $item_ids)->get(['item_id', 'is_available']);

The answer is thus:

{
  "items" : {[
    0: {
      "item_id": 1,
      "is_available": true;
    },
    1: {
      "item_id": 2,
      "is_available": true;
    }
  ]}
}

item id 4 has been ignored as it does not exist in the database, what I need is a default answer for when there is no item, for example:

{
  "items" : {[
    0: {
      "item_id": 1,
      "is_available": true;
    },
    1: {
      "item_id": 2,
      "is_available": true;
    },
    2: {
      "item_id": 4,
      "is_available": false;
    },
  ]}
}

2 answers

0

I would use another logic, map the items and return with the collection structure you need

$itemsAvaiable = Item::whereIn('item_id', $item_ids)->get(['item_id', 'is_available']);

$itemsNotAvaiable = collect($item_ids)->map(function ($item) use ($itemsAvaiable) {
    return [
        'item_id' => $item,
        'is_avaiable' => $itemsAvaiable->contains('item_id', $item)
    ];
});

$items = $itemsAvaiable->merge($itemsNotAvaiable);

0


Following the same line of reasoning of @Ademir-Mazer-Jr-One I will leave a reply to enrich the post.

https://paste.laravel.io/873076f6-e386-4222-b225-44bd15b6aa97

$item_ids = [1, 2, 4];
 
//simulando itens do resultado do whereIn
$items = [
    [
         "item_id" =>  1,
         "is_available" =>  true
    ],
    [
         "item_id" =>  2,
         "is_available" =>  true
    ]
];
 
//simulando itens do resultado do whereIn
$itemsFromDB = collect($items);
$itemsFromDBPluck = $itemsFromDB->pluck('item_id')->toArray();
 
collect($item_ids)->map(function ($item) use ($itemsFromDB, $itemsFromDBPluck) {
    if (!in_array($item, $itemsFromDBPluck)) {
        $appendItem = [ 'item_id' => $item,'is_avaiable' => false];
        $itemsFromDB->push($appendItem);
    }
});
dump($itemsFromDB);//$itemsFromDB->toArray();

And using Where:

collect($item_ids)->map(function ($item) use ($itemsFromDB) {
    ///dump($item);
    $findItem = $itemsFromDB->where('item_id',$item);
    if ($findItem->isEmpty()) {
        $appendItem = [ 'item_id' => $item,'is_avaiable' => false];
        $itemsFromDB->push($appendItem);
    }
});
dump($itemsFromDB);//$itemsFromDB->toArray();

The output of both examples will be:

Illuminate\Support\Collection {#268 ▼
  #items: array:3 [▼
    0 => array:2 [▼
      "item_id" => 1
      "is_available" => true
    ]
    1 => array:2 [▼
      "item_id" => 2
      "is_available" => true
    ]
    2 => array:2 [▼
      "item_id" => 4
      "is_avaiable" => false
    ]
  ]
}
  • Good Marcos, I voted a positive p vc to try to minimize the absolutely unfounded negatives that this Stackoverflow community in Portuguese loves. This here is getting worse and worse, our answers are legitimate solutions to the question and the question itself is totally in agreement, but here is avalanche of negatives ... seriously thinking of abandoning the Stack PT BR

  • I don’t even complain more about down votes, I still enter to help people because one day I was beginner (of course I am in constant learning) and I like to share some knowledge. No down votes could at least describe the reason.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.