简体   繁体   中英

Laravel 5.2 Update MySQL Database from MongoDB JSON Response

How do i update a table in a MySQL Database from data that I'm pulling from a MongoDB?

I have a function that pulls the data from MongoDB

$recordid = '9depnuDz1XHl';

$mongodata = MongoDataPull::find($recordid);

When i run the following:

return [$mongodata];

This is the response i get:

[
    {
        "_id": "9depnuDz1XHl",
        "name": "Elliot",
        "surname": "Alderson",
        "email": "elliot@fsociety.com",
        "cell": "01239871234",
        "client": false,
        "payment": false,
        "user_id": "EVhTgHsh1H0A",
        "theme": "default"
    }
]

My column names in MySQL are slightly different from the ones from the response.

MySQL Columns in table:

fname
lname
email
phone
client
payment
userid
theme

Using Eloquent, how would i write a query to update MySQL with the data from MongoDB while mapping to the correct column?

If you use fill() , make sure to have either $guarded or $fillable on the model. Also, find() will only work if that is the primary ID. Otherwise, use where('primary_key', "=", $mongodata->_id)->first();

$MysqlModel = Model::find($mongodata->_id);

$MysqlModel->fill(['fname' => $mongodata->name, etc...]);

$MysqlModel->save();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM