PHPackages                             mtownsend/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. [API Development](/categories/api)
4. /
5. mtownsend/collection-xml

ActiveLibrary[API Development](/categories/api)

mtownsend/collection-xml
========================

The missing XML support for Laravel's Collection class.

1.10.0(1mo ago)4443.0k↓12.9%5[1 issues](https://github.com/mtownsend5512/collection-xml/issues)MITPHPPHP ~7.0|~8.0|~8.1CI failing

Since Oct 16Pushed 1mo ago2 watchersCompare

[ Source](https://github.com/mtownsend5512/collection-xml)[ Packagist](https://packagist.org/packages/mtownsend/collection-xml)[ RSS](/packages/mtownsend-collection-xml/feed)WikiDiscussions master Synced yesterday

READMEChangelog (10)Dependencies (6)Versions (16)Used By (0)

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

61

—

FairBetter than 98% of packages

Maintenance92

Actively maintained with recent releases

Popularity41

Moderate usage in the ecosystem

Community16

Small or concentrated contributor base

Maturity78

Established project with proven stability

 Bus Factor1

Top contributor holds 83.3% 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 ~198 days

Recently: every ~313 days

Total

15

Last Release

37d 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.5.0PHP ~7.0|~8.0|~8.1

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/812987?v=4)[mtownsend](/maintainers/mtownsend)[@mtownsend](https://github.com/mtownsend)

---

Top Contributors

[![mtownsend5512](https://avatars.githubusercontent.com/u/4945553?v=4)](https://github.com/mtownsend5512 "mtownsend5512 (30 commits)")[![laravel-shift](https://avatars.githubusercontent.com/u/15991828?v=4)](https://github.com/laravel-shift "laravel-shift (3 commits)")[![bagart](https://avatars.githubusercontent.com/u/2109022?v=4)](https://github.com/bagart "bagart (1 commits)")[![eviweb](https://avatars.githubusercontent.com/u/1310382?v=4)](https://github.com/eviweb "eviweb (1 commits)")[![thanonoc](https://avatars.githubusercontent.com/u/31168024?v=4)](https://github.com/thanonoc "thanonoc (1 commits)")

---

Tags

apiarraycollectionslaravellaravel-packageresponse-formatsoaptoxmlxmllaravelxmlcollectionsoaptoXMLtoSoapXml

###  Code Quality

TestsPHPUnit

### Embed Badge

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

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

###  Alternatives

[defstudio/telegraph

A laravel facade to interact with Telegram Bots

816333.6k3](/packages/defstudio-telegraph)[mtownsend/response-xml

The missing XML support for Laravel's Response class.

1041.3M4](/packages/mtownsend-response-xml)[andreaselia/laravel-api-to-postman

Generate a Postman collection automatically from your Laravel API

1.0k644.5k4](/packages/andreaselia-laravel-api-to-postman)[simplestats-io/laravel-client

Server-side analytics for Laravel that follows the full funnel from visit to registration to payment, attributed to the channel that drove it. Revenue, MRR, churn and ad-spend profit (ROAS/CAC) per channel. GDPR compliant, ad-blocker proof.

5021.9k](/packages/simplestats-io-laravel-client)

PHPackages © 2026

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