Generate xml file from Laravel template
As per the Laravel Documentation, Collections and their relationships can be output to arrays:
$roles = User::find(1)->roles->toArray();
for example, I have two models, User
and Phone
, and a user hasMany()
phones.
users phones
+----+--------+ +----+-----------+---------+
| id | name | | id | number | user_id |
+----+--------+ +----+-----------+---------+
| 1 | user 1 | | 1 | 111111111 | 1 |
| 2 | user 2 | | 2 | 222222222 | 2 |
+----+--------+ | 3 | 333333333 | 1 |
+----+-----------+---------+
we can return an array of this using the toArray()
method, and with()
to pull out all the related Phones
:
$users = User::with('phones')->get()->toArray();
giving this (I have hidden
some of the fields on the model for the purposes of this answer):
Array (
[0] => Array (
[id] => 1
[name] => user 1
[phones] => Array (
[0] => Array (
[id] => 1
[number] => 111111111
[user_id] => 1
)
[1] => Array (
[id] => 3
[number] => 333333333
[user_id] => 1
)
)
)
[1] => Array (
[id] => 2
[name] => user 2
[phones] => Array (
[0] => Array (
[id] => 2
[number] => 222222222
[user_id] => 2
)
)
)
)
Then you can use any of the various methods for turning arrays into XML. Here's one I've lifted from another answer on SO
function array_to_xml(array $arr, SimpleXMLElement $xml)
{
foreach ($arr as $k => $v) {
is_array($v)
? array_to_xml($v, $xml->addChild($k))
: $xml->addChild($k, $v);
}
return $xml;
}
Example output:
<?xml version="1.0"?>
<root><0><id>1</id><name>user 1</name><phones><0><id>1</id><number>111111111</number><user_id>1</user_id></0><1><id>3</id><number>333333333</number><user_id>1</user_id></1></phones></0><1><id>2</id><name>user 2</name><phones><0><id>2</id><number>222222222</number><user_id>2</user_id></0></phones></1></root>
Comments
-
Anastasie Laurent almost 2 years
I have a Laravel template and I want to generate an XML file depending on it.
For example, the structure has a name, XPath and type. So the XML should look like this:
<name> </name> <type> </type> <xpath> </xpath>
Of course I am looking for something more complex that can deal with relationships.
Is there any tool for that?
I am using MySQL, so maybe there is a tool for that, too? Or maybe a PHP script?
I already tried to search and all I have found is a tool to generate HTML forms from XSD and general XML from XSD.
Update
The tables and their columns are:
-
xml_document
table with columns:id
-
general_information
table with columns:id
,xml_document_id
,domain
,start_url
and `category -
master_information
table with columns:id
,xml_document_id
,container
, andnext_page
-
master_attribute
table with columns:id
,master_information_id
,name
,xpath
, andtype
-
details_attribute
table with columns:id
,xml_document_id
, andtype
As you may notice, the relationships between:
- xml_document and master_information is one to one.
- xml_document and general_information is one to one.
- xml_document and details_attribute is one to many.
- master_information and master_attribute is one to many
-
-
Anastasie Laurent almost 10 yearsvery nice answer, I will try it and update you. but after 1.30 hour. many thanks
-
Anastasie Laurent almost 10 yearswhen I tried this:
$xmlDocument = XmlDocument::find(4)->get()->toArray();
I got all the rows in the database not just the row4
. then I tried this$xmlDocument = XmlDocument::find(4)->toArray();
so I just get the columns not the relationships. I mean I just get the id(witch is only column in the table xmldocument). However, I should have got the other relationships. for exaple, I have one to many relationship with a tablemaster_information
, I didn't get these values. could you help please? -
msturdy almost 10 years@AnastasieLaurent - can you update your question with the relationships ?
-
Anastasie Laurent almost 10 yearsOkay I will give you all the relationships, wait please to update the question
-
Anastasie Laurent almost 10 yearsuntil now I have this code
$xmlDocument = XmlDocument::with('MasterInformation', 'GeneralInformation', 'DetailsAttributes')->find(4)->toArray();
and it gives me all the information. I just still have to add theMasterAttribute
to this question. I didn't know how. that is because theMasterAttribute
is one to many withMasterInformation
not withXmlDocument
can you help please? -
Anastasie Laurent almost 10 yearshere we go
$xmlDocument = XmlDocument::with('MasterInformation', 'GeneralInformation', 'MasterInformation.MasterAttributes', 'DetailsAttributes')->find(4)->toArray();
it works, now I will check your code that transfer the array to xml. -
Anastasie Laurent almost 10 yearsThat is enough for this question. I will accept your answer and then ask another answer about changing array to xml. will give you the link in case you would like to answer. appreicate your help
-
Anastasie Laurent almost 10 yearsI created a new account because I couldn't ask more than 50 questions each 30 days and I asked this question stackoverflow.com/questions/24556875/… can you check pleaes?