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;
},
]}
}
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
– Ademir Mazer Jr - Nuno
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.
– Marcos Xavier