firstOrCreate on multiple related records from XML feed

Posted 1 month ago by codebullet

Hi all please I need help saving multiple records from XML Feed

I am using Rodenastyle/stream-parser to receive the data, I can do updateOrCreate the records My challenge is this record has some data I want to save in a new model with the relationship

Saving data without the relationship works fine but the issue is the multiple relationships involve and getting the IDs of the related model to add to the property Table

This is the data i am receiving

<?xml version="1.0" encoding="UTF-8"?>
<list last_update="2019-06-29 15:51:39" listing_count="442">
    
    <property last_update="2019-04-30 10:02:17">
        <permit_number>003000000</permit_number>
        <reference_number>XX-XXXXX</reference_number>
        <offering_type>RS</offering_type>
        <property_type>AP</property_type>
        <price_on_application>No</price_on_application>
        <price>1530000</price>
        <city><![CDATA[ City ]]></city>
        <community><![CDATA[ Community ]]></community>
        <sub_community><![CDATA[ Sub Community ]]></sub_community>
        <title_en><![CDATA[  What is Lorem Ipsum?  ]]></title_en>
        <description_en><![CDATA[
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Nam posuere neque at ligula vulputate, eget efficitur est posuere.
        Vivamus suscipit lorem a turpis porta, vitae pellentesque ligula egestas.
        Etiam ullamcorper tortor a lorem cursus consectetur id ac leo.

        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
        ]]></description_en>
        <amenities>BA,BP,BB,AN,BW,CP,FF,GZ,PJ,BK,MR,SA,SP,SR,UI,GR,SE,MT,CV,NS,NM,SM,PT,MO,VT,PK,RT,CS,SY,WC,HT</amenities>
        <size>772</size>
        <bedroom>1</bedroom>
        <bathroom>2</bathroom>
        <agent>
            <name><![CDATA[Yasir Javed]]></name>
            <email>[email protected]</email>
            <phone>+555 55 555 5555</phone>
            <license_no>36196</license_no>
        </agent>
        <developer>Developer</developer>
        <parking>1</parking>
        <furnished>No</furnished>
        <view360>https://youtu.be</view360>
        <photo>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
        </photo>
        <geopoints>55.273,25.19</geopoints>
    </property>

        <property last_update="2019-04-30 10:02:17">
        <permit_number>003000000</permit_number>
        <reference_number>XX-XXXXX</reference_number>
        <offering_type>RS</offering_type>
        <property_type>AP</property_type>
        <price_on_application>No</price_on_application>
        <price>1530000</price>
        <city><![CDATA[ City ]]></city>
        <community><![CDATA[ Community ]]></community>
        <sub_community><![CDATA[ Sub Community ]]></sub_community>
        <title_en><![CDATA[  What is Lorem Ipsum?  ]]></title_en>
        <description_en><![CDATA[
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Nam posuere neque at ligula vulputate, eget efficitur est posuere.
        Vivamus suscipit lorem a turpis porta, vitae pellentesque ligula egestas.
        Etiam ullamcorper tortor a lorem cursus consectetur id ac leo.

        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
        ]]></description_en>
        <amenities>BA,BP,BB,AN,BW,CP,FF,GZ,PJ,BK,MR,SA,SP,SR,UI,GR,SE,MT,CV,NS,NM,SM,PT,MO,VT,PK,RT,CS,SY,WC,HT</amenities>
        <size>772</size>
        <bedroom>1</bedroom>
        <bathroom>2</bathroom>
        <agent>
            <name><![CDATA[Yasir Javed]]></name>
            <email>[email protected]</email>
            <phone>+555 55 555 5555</phone>
            <license_no>36196</license_no>
        </agent>
        <developer>Developer</developer>
        <parking>1</parking>
        <furnished>No</furnished>
        <view360>https://youtu.be</view360>
        <photo>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
        </photo>
        <geopoints>55.273,25.19</geopoints>
    </property>


        <property last_update="2019-04-30 10:02:17">
        <permit_number>003000000</permit_number>
        <reference_number>XX-XXXXX</reference_number>
        <offering_type>RS</offering_type>
        <property_type>AP</property_type>
        <price_on_application>No</price_on_application>
        <price>1530000</price>
        <city><![CDATA[ City ]]></city>
        <community><![CDATA[ Community ]]></community>
        <sub_community><![CDATA[ Sub Community ]]></sub_community>
        <title_en><![CDATA[  What is Lorem Ipsum?  ]]></title_en>
        <description_en><![CDATA[
        Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.


        Lorem ipsum dolor sit amet, consectetur adipiscing elit.
        Nam posuere neque at ligula vulputate, eget efficitur est posuere.
        Vivamus suscipit lorem a turpis porta, vitae pellentesque ligula egestas.
        Etiam ullamcorper tortor a lorem cursus consectetur id ac leo.

        Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur
        ]]></description_en>
        <amenities>BA,BP,BB,AN,BW,CP,FF,GZ,PJ,BK,MR,SA,SP,SR,UI,GR,SE,MT,CV,NS,NM,SM,PT,MO,VT,PK,RT,CS,SY,WC,HT</amenities>
        <size>772</size>
        <bedroom>1</bedroom>
        <bathroom>2</bathroom>
        <agent>
            <name><![CDATA[Yasir Javed]]></name>
            <email>[email protected]</email>
            <phone>+555 55 555 5555</phone>
            <license_no>36196</license_no>
        </agent>
        <developer>Developer</developer>
        <parking>1</parking>
        <furnished>No</furnished>
        <view360>https://youtu.be</view360>
        <photo>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
            <url last_update="2018-05-10 12:13:46" watermark="Yes">http://example.com/example.jpg</url>
        </photo>
        <geopoints>55.273,25.19</geopoints>
    </property>
    
    
</list>  







This is my code, please advise the best approach on how to save these multiple records 


public function xml(){
        StreamParser::xml("https://fidu.mycrm.com/feed/privatesite/aa1b5f59a538a14cca22a9e7c659cc3a")->each(function (\Tightenco\Collect\Support\Collection $property){
            $property = Property::updateOrCreate(
                [
                    'reference_number' => $property->get('reference_number'),
                ],
                [
                    'title' => $property->get('title_en'),
                    'meta_title' => $property->get('title_en'),
                    'permit_number' => (int)$property->get('permit_number'),
                    'bedroom' => $property->get('bedroom'),
                    'bathroom' => $property->get('bedroom'),
                    'parking' => (int)$property->get('parking'),
                    'description' => nl2br($property->get('description_en')),
                    'price'=> $property->get('price'),
                    'property_type' => (int)$property->get('property_type'),
                    'offering_type' => $property->get('offering_type'),
                    'size' => $property->get('size'),
                    'slug' => SlugService::createSlug(Property::class, 'slug', $property->get('title_en')),
                    'gallery' => '', // I want to loop through the photos and save to App\Gallery Model,
                    'agent' => '', //I want to firstOrCreate the agent by agent license_number to App\Agent Model,
                    'Community' => '', //I have a community Model i need to firstOrCreate on App\Community "name" field


                ],

                // I am trying to set the property community here, but i face issue returning the ID of the community
                $property->community = Community::firstOrCreate(
                [
                    'name' => $property->get('community'),
                ],
                [
                    'city_id' => '',
                    'description'  => '',
                    'image' => ''
                ])

                    //Getting the Agent's Data
                    
                    
                    
                    
                    
                    
                    //saving the Agent
                  

            );


            // Save Property with Agent_id, Community_id, Subcommunity_id and gallery_id
//            $property->save();
//            return view('manager.properties.index')->with('status', 'Properties Created Successfully ');
        });
    }

I don't' have much experience on this, any guide or heads up will help

Thanks very much for reading I appreciate

Please sign in or create an account to participate in this conversation.