lundi 28 mars 2016

Check if record exists in database using Laravel 5.1

I am inserting an xml file contents into a DB from my localstorage. However what happens is that the xml file can be updated and when its updated i need to check if the record already exists or not in my DB. If it exists i need to update the record and if it doesn't exist i need to insert a new record. I am able to do the insert but I dont seem to find a Laravel way to do this using update method.

In my xml file they is a node that doesn't change value when the xml has been updated. I am thinking I must use an if statement to check against this node's value but I cant seem to get my head around this.

Below is how I am doing my insert.

public function store(Request $request)
  {
    // Building directory path.
    $directory = storage_path('app/xmlentries/uploads');

    $files = File::allFiles($directory);

    foreach($files as $file) {

    $contents = $file->getContents();

    foreach((array) $contents as $content)
    {
        $simpleXml = simplexml_load_string($xml);

        $data = [
        'requisition' => $simpleXml->ExportData->Requisition['clientReqId'], //NODE THAT DOES NOT CHANGE VALUE
        'experience' => $simpleXml->ExportData->Requisition->EssentialFunction,
        'job_requirements' => $simpleXml->ExportData->Requisition->EssentialFunction,
        ],
        ];

        Vacancy::insert($data); //WHERE I AM INSERTING MY RECORD

    }

    }
    }   

How would I update the record if it already exists?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire