# qb-inventory

{% tabs %}
{% tab title="Old Version" %}
Add the following to the end of the `qb-inventory/server/main.lua` file

```lua
exports("GetStash", function(stashId)
    return Stashes[stashId] or {
        items = GetStashItems(stashId)
    }
end)

exports('GetStashItems', GetStashItems)

RegisterServerEvent('qb-inventory:server:SaveStashItems', function(stashId, items)
    MySQL.insert('INSERT INTO stashitems (stash, items) VALUES (@stash, @items) ON DUPLICATE KEY UPDATE items = @items', {
        ['@stash'] = stashId,
        ['@items'] = json.encode(items)
    })

    if Stashes[stashId] then
        Stashes[stashId].items = items
    end
end)
```

{% endtab %}

{% tab title="New Version" %}
In the `qb-inventory/server/functions.lua` file we will modify and add the following:

• The function `InitializeInventory(inventoryId, data)` will be left as follows

```lua
local function InitializeInventory(inventoryId, data)
    if Inventories[inventoryId] and Inventories[inventoryId].maxweight then return Inventories[inventoryId] end
    Inventories[inventoryId] = {
        items = {},
        isOpen = false,
        label = data and data.label or inventoryId,
        maxweight = data and data.maxweight or Config.StashSize.maxweight,
        slots = data and data.slots or Config.StashSize.slots
    }
    return Inventories[inventoryId]
end
```

• Below the function we export it by adding the following code

```lua
exports('InitializeInventory', InitializeInventory) -- exports['qb-inventory']:InitializeInventory(inventoryId, data) 
```

• And finally we add this function and export it.

```lua
local function getItemCountByName(identifier, item)
    if not Inventories[identifier] then return 0 end
    local count = 0
    for _, v in pairs(Inventories[identifier].items) do
        if v and v.name == item then
            count = count + v.amount
        end
    end
    return count
end

exports('getItemCountByName', getItemCountByName) -- exports['qb-inventory']:getItemCountByName(identifier, item)
```

{% endtab %}
{% endtabs %}
