Weave Code
Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Collection Xml Laravel Package

mtownsend/collection-xml

mtownsend/collection-xml adds a simple toXml() macro to Laravel collections and arrays, making it easy to convert nested data into clean XML for feeds, APIs, exports, and legacy integrations.

View on GitHub
Deep Wiki
Context7

Getting Started

  1. Install: composer require mtownsend/collection-xml
  2. Laravel 5.5+ auto-discovers the service provider. On Lumen or older Laravel, register Mtownsend\CollectionXml\CollectionXmlServiceProvider::class in config/app.php.
  3. Look here first: vendor/mtownsend/collection-xml/src. The README is short and the package is small — the service provider is where the macros are registered, and it's the fastest way to confirm the exact method signatures and options for the version you installed.
  4. Smoke test in php artisan tinker:
collect(['user' => ['name' => 'Ada', 'role' => 'admin']])->toXml();

If you get an XML string back, you're wired up.

First real use case: exporting an Eloquent result set as an XML feed, or ingesting an XML webhook body into a collection so the rest of your app only deals with arrays/collections.

Implementation Patterns

Export (collection → XML). The macro lives on every Collection instance. The day-to-day shape is query → map to plain arrays → toXml() → store or return.

$xml = User::query()->get()
    ->map(fn ($u) => ['id' => $u->id, 'email' => $u->email])
    ->toXml();

Storage::put('exports/users.xml', $xml);

Import (XML → collection). Parse once at the boundary — controller, job, or a small XmlPayload DTO — then pass the collection everywhere else. Don't leak raw XML strings into your domain layer.

$payload = Collection::fromXml($request->getContent());
$orders  = $payload->get('orders.order', collect());

Round-tripping. Because both directions yield collections, normalization is just fromXml($in)->map(...)->toXml(). This is the sweet spot: reshaping a third-party XML format into your own.

Wrap it in a service class. Put root element name, declaration, and array shaping in one app-level class. With a single-maintainer, 44-star package, this is your insurance policy — swapping implementations later becomes a one-file change.

HTTP layer pairing. Combine with mtownsend/request-xml (XML request body → array) and mtownsend/response-xml (XML responses) so controllers stay thin and collection-xml only handles the array↔XML hop.

Testing. Assert on parsed XML, not strings: simplexml_load_string($xml) and check nodes. String assertions break on whitespace and attribute ordering.

Gotchas and Tips

  • Exactly one root element. Nested associative arrays become child elements, but top-level scalars or lists need a wrapper. Always specify/verify a root name rather than trusting the default.
  • List arrays become repeated tags. ['item' => [1,2,3]] produces three <item> elements — but fromXml can't tell a one-element list from a scalar. Normalize explicitly after parsing.
  • Keys become element names. Keys with spaces, leading digits, &, or : yield invalid or namespaced XML. Sanitize or map keys before calling toXml().
  • No attributes, CDATA, namespaces, or mixed content. This is a convenience serializer, not a DOM. If a consumer requires <user id="1"> or XSD-valid output, build it with DOMDocument/XMLWriter instead.
  • Nulls and empty arrays. Verify how your version renders null and [] (self-closing vs. omitted) with a test before shipping to anything schema-validated.
  • Escaping is delegated. If output looks mangled, dd() the raw string first — it's almost always an upstream value containing markup, not the package.
  • Encoding. Force UTF-8 end-to-end (DB, HTTP headers, mb_*). Emoji and non-ASCII are the usual source of mojibake or parse failures.
  • Memory. toXml() builds the entire string in memory. For big exports, chunk() and append, or stream with XMLWriter.
  • Debugging. Pipe output through xmllint --format - locally; a false from simplexml_load_string() plus libxml_get_errors() pinpoints the bad node instantly.
  • Extension points. Register your own macros for repeated shapes (e.g. ->toSitemapXml()), and check php artisan vendor:publish after install for a config file where defaults like root name live.
  • Maintenance risk. Tiny package, low visibility, but equally tiny surface area — the wrapper-class pattern above keeps that risk contained.
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
3mdikram/worldmoneywords
snowsoft/config
itmosoft/laravel-subscriptions
larva/laravel-ai-qwen
diephp/sequences
users-au/socialite-provider
twidpay/twid-logger
mrnewport/laravel-plaid
mrnewport/laravel-repo
mrnewport/laravel-docsign
mrnewport/laravel-stow
mrnewport/laravel-flow
mrnewport/laravel-priceable
mrnewport/laravel-nestedterms
fredmeng/laravel-apidoc-generator
itstructure/laravel-multilingual-tools
pdustdar/elasticsearch
markupai/php-sdk
rlacerda83/lumem-microservice-shipment
uni-method/symfony-lib