Generate xml file from Laravel template

Stefan Bogdanescu

Founder & Senior Architect · 2026-06-29

Laravel Company

Generating Complex XML from Relational Data in Laravel: A Developer's Guide

When working with relational databases like MySQL, translating complex data structures into a standardized format like XML can seem daunting. Especially when dealing with one-to-many and one-to-one relationships, simply exporting rows is not enough; you need a thoughtful strategy to structure the hierarchy correctly. As a senior developer, I often encounter this challenge when building APIs or generating specific file formats from data stored in Laravel applications.

The request to generate an XML file based on your defined tables (xml_document, master_information, etc.) is a classic data transformation problem. While there are tools available that handle simple XSD conversions, they often fall short when dealing with intricate database relationships. The solution, in the PHP/Laravel ecosystem, lies in leveraging Eloquent relationships and custom recursive logic to build the XML structure programmatically.

This guide will walk you through the developer-centric approach to solving this problem, focusing on how to map your MySQL structure into clean, hierarchical XML.

Why Custom Scripting is Necessary for Relationships

Generic tools often focus on schema validation (like XSD) rather than dynamic data fetching and nesting. Since your requirement involves specific one-to-one and one-to-many relationships—such as xml_document having many details_attribute records—a custom script offers the necessary control to define the exact XML hierarchy you need.

In a Laravel context, we utilize Eloquent models to fetch the related data efficiently. Instead of manually writing complex SQL joins and then trying to format the result, we delegate the relationship management to the ORM, which makes the process cleaner and more maintainable. This aligns perfectly with the principles of building robust applications, much like how high-quality frameworks like those promoted by Laravel encourage clean separation of concerns.

The Implementation Strategy: Eloquent and Recursive Mapping

To handle your complex structure effectively, we need a strategy that starts from the main entity (xml_document) and recursively pulls in all related data. We will use PHP to iterate through the primary records and build the XML tree node by node.

Step 1: Define Eloquent Relationships

First, ensure your Laravel models correctly define the relationships based on your foreign keys. For instance, an XmlDocument model would need relationships defined for its one-to-one and one-to-many associations.

// Example structure in a Laravel Model (e.g., XmlDocument.php)
class XmlDocument extends Model
{
    public function generalInformation()
    {
        return $this->hasOne(GeneralInformation::class);
    }

    public function masterInformation()
    {
        return $this->hasOne(MasterInformation::class);
    }

    public function detailsAttributes()
    {
        return $this->hasMany(DetailsAttribute::class);
    }
}

Step 2: The XML Generation Logic (PHP Example)

We will write a service or controller method to handle the data fetching and XML serialization. This involves fetching the main entity, then iterating through its relationships, and finally nesting the results into the desired XML format.

Here is a conceptual example demonstrating how you might start building the structure in PHP:

<?php

use App\Models\XmlDocument;

class XmlGenerator
{
    public function generate(int $documentId)
    {
        $document = XmlDocument::with('generalInformation', 'masterInformation', 'detailsAttributes')
                                ->find($documentId);

        if (!$document) {
            return null;
        }

        $xml = new SimpleXMLElement('<xml_document/>');
        
        // 1. Add simple fields from xml_document
        $xml->addChild('name', $document->name);
        $xml->addChild('type', $document->type);
        $xml->addChild('xpath', $document->xpath);

        // 2. Handle One-to-One Relationships (General Information)
        $general = $document->generalInformation;
        if ($general) {
            $xml->addChild('general_information', '');
            $xml->general_information->addChild('domain', $general->domain);
            $xml->general_information->addChild('start_url', $general->start_url);
        }

        // 3. Handle One-to-Many Relationships (Details Attributes)
        foreach ($document->detailsAttributes as $attribute) {
            $xml->addChild('detail_attribute', '');
            $xml->detail_attribute->addChild('type', $attribute->type);
        }

        // 4. Handle another One-to-One relationship (Master Information)
        $master = $document->masterInformation;
        if ($master) {
             $xml->addChild('master_information', '');
             $xml->master_information->addChild('container', $master->container);
             $xml->master_information->addChild('next_page', $master->next_page);
        }

        // Output the final XML structure
        return $xml->asXML();
    }
}

This approach ensures that complex relationships are nested logically. For instance, all details_attribute nodes will be children of the main <xml_document>, and related master data will be correctly placed within their respective parent nodes.

Conclusion

Generating complex XML from a relational database requires moving beyond simple export tools and embracing the power of your framework. By utilizing Eloquent relationships alongside custom PHP logic, you gain full control over the output structure, ensuring that one-to-one and one-to-many relationships are accurately represented in the final XML file. This method is robust, scalable, and perfectly suited for complex data transformations within a Laravel environment.