PHPackages                             bagart/collection-xml - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Utility &amp; Helpers](/categories/utility)
4. /
5. bagart/collection-xml

ActiveLibrary[Utility &amp; Helpers](/categories/utility)

bagart/collection-xml
=====================

The missing XML support for Laravel's Collection class.

1.4.0.2(4y ago)0184MITPHPPHP ~7.0|^8.0

Since Oct 16Pushed 4y agoCompare

[ Source](https://github.com/bagart/collection-xml)[ Packagist](https://packagist.org/packages/bagart/collection-xml)[ RSS](/packages/bagart-collection-xml/feed)WikiDiscussions master Synced 1w ago

READMEChangelog (1)Dependencies (3)Versions (12)Used By (0)

Fork fixes
----------

[](#fork-fixes)

- Add PHP8/Laravel8 support

The missing XML support for Laravel's Collection class.

This package is designed to work with the [Laravel](https://laravel.com) framework.

Installation
------------

[](#installation)

Install via composer:

```
composer require mtownsend/collection-xml

```

### Registering the service provider

[](#registering-the-service-provider)

For Laravel 5.4 and lower, add the following line to your `config/app.php`:

```
/*
 * Package Service Providers...
 */
Mtownsend\CollectionXml\Providers\CollectionXmlServiceProvider::class,
```

For Laravel 5.5 and greater, the package will auto register the provider for you.

### Using Lumen

[](#using-lumen)

To register the service provider, add the following line to `app/bootstrap/app.php`:

```
$app->register(Mtownsend\CollectionXml\Providers\CollectionXmlServiceProvider::class);
```

Quick start
-----------

[](#quick-start)

### Collection to xml

[](#collection-to-xml)

Convert data inside a Laravel Collection into valid XML:

```
$collection = collect([
    'carrier' => 'fedex',
    'id' => 123,
    'tracking_number' => '9205590164917312751089',
]);

$xml = $collection->toXml();

// Returns

    fedex
    123
    9205590164917312751089

```

### Collection to soap xml

[](#collection-to-soap-xml)

*Heads up, this method is the most temperamental part of this package. Please thoroughly check your SOAP endpoint and requirements for best usage.*

```
$collection = collect([
    'carrier' => 'fedex',
    'id' => 123,
    'tracking_number' => '9205590164917312751089',
]);

$xml = $collection->toSoapXml('request', 'xmlBody', 'https://yourwebserver/service.asmx?wsdl');

// Returns

                    fedex
                    123
                    9205590164917312751089

```

**Please note:** the SoapFactory class will ping the `$fullUrl` to see if it is valid as it builds the SOAP xml. It will not trigger an api interaction, but you will experience an exception if your url is invalid.

### Array to xml

[](#array-to-xml)

Convert an array into xml without using a collection:

```
$array = [
    'carrier' => 'fedex',
    'id' => 123,
    'tracking_number' => '9205590164917312751089',
];

$xml = array_to_xml($array);
```

Helpers, methods, and arguments
-------------------------------

[](#helpers-methods-and-arguments)

**Helper**

`array_to_xml($array, $root = '')`

The `$root` argument allows you to customize the root xml element. Default is ``.

**Collection method**

`->toXml($root)`

See `array_to_xml()` above.

**Collection method**

`->toSoapXml($root = '', $soapRoot = '', $fullUrl, array $options = [])`

The `$root` argument allows you to customize the inner root xml element. Default is ``.

`$soapRoot` is the outer xml root element. Default is `xmlBody`.

`$fullUrl` will be the fully qualified SOAP endpoint e.g. . **Please note:** the SoapFactory class will ping the `$fullUrl` to see if it is valid as it builds the SOAP xml. It will not trigger an api interaction, but you will experience an exception if your url is invalid.

`$options` will be an array of valid options for PHP's [SoapClient](http://php.net/manual/en/class.soapclient.php) class. By default `['trace' => 1]` is set.

Purpose
-------

[](#purpose)

Laravel has always favored json over xml with its api structure. Inevitably, developers will be required to interact with files or apis that require xml, and they are often left to figure it out for themselves.

This package aims to bring painless xml support to Laravel's Collection class, and bring a few useful helpers along.

Other packages you may be interested in
---------------------------------------

[](#other-packages-you-may-be-interested-in)

- [mtownsend/request-xml](https://github.com/mtownsend5512/request-xml)
- [mtownsend/response-xml](https://github.com/mtownsend5512/response-xml)
- [mtownsend/xml-to-array](https://github.com/mtownsend5512/xml-to-array)

Credits
-------

[](#credits)

- Mark Townsend
- [Spatie](https://spatie.be/)
- [All Contributors](../../contributors)

Testing
-------

[](#testing)

You can run the tests with:

```
./vendor/bin/phpunit
```

License
-------

[](#license)

The MIT License (MIT). Please see [License File](LICENSE.md) for more information.

###  Health Score

31

—

LowBetter than 68% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity10

Limited adoption so far

Community8

Small or concentrated contributor base

Maturity73

Established project with proven stability

 Bus Factor1

Top contributor holds 82.1% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~134 days

Recently: every ~176 days

Total

10

Last Release

1558d ago

Major Versions

0.0.1 → 1.0.02018-10-17

PHP version history (3 changes)0.0.1PHP ~7.0

1.4.0PHP ~7.0|~8.0

1.4.0.2PHP ~7.0|^8.0

### Community

Maintainers

![](https://www.gravatar.com/avatar/02a582356aa07935e42e622817f56810dce45be995af444215fabb072a39992b?d=identicon)[bagart](/maintainers/bagart)

---

Top Contributors

[![mtownsend5512](https://avatars.githubusercontent.com/u/4945553?v=4)](https://github.com/mtownsend5512 "mtownsend5512 (23 commits)")[![bagart](https://avatars.githubusercontent.com/u/2109022?v=4)](https://github.com/bagart "bagart (5 commits)")

---

Tags

laravelxmlcollectionsoaptoXMLtoSoapXml

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/bagart-collection-xml/health.svg)

```
[![Health](https://phpackages.com/badges/bagart-collection-xml/health.svg)](https://phpackages.com/packages/bagart-collection-xml)
```

###  Alternatives

[mtownsend/collection-xml

The missing XML support for Laravel's Collection class.

4440.3k](/packages/mtownsend-collection-xml)[lazerg/laravel-enum-pro

A powerful PHP enum extension with collection support, random selection, and magic static calls

4319.0k](/packages/lazerg-laravel-enum-pro)[werxe/laravel-collection-macros

Custom Laravel Collection macros.

2625.8k](/packages/werxe-laravel-collection-macros)[interaction-design-foundation/laravel-geoip

Support for multiple Geographical Location services.

17221.0k3](/packages/interaction-design-foundation-laravel-geoip)[prologue/support

Prologue Support is an extension for Illuminate Support

1616.8k](/packages/prologue-support)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
