Wednesday, March 15, 2017

Eloquent - Strange behavior on save

I'm saving a record and then returning the values from database and Eloquent is not returning all of the values.

Example:

If I have a MySQL table called names like this:

| ID                         | Date                                | Name         |
|----------------------------|-------------------------------------|--------------|
| AUTO INCREMENT PRIMARY KEY | TIMESTAMP DEFAULT CURRENT_TIMESTAMP | VARCHAR(100) |

and I have an eloquent model called Names

and then I save and return like this:

function save($sName) {
    $oName = new Names();
    $oName->Name = $sName;

    $oName->save();

    $oReturn = new stdClass();
    $oReturn->Name = $oName->Name;
    $oReturn->Date = $oName->Date;
    return $oReturn;
}

It returns:

echo json_encode(save("Test")); // {"Name": "Test", "Date": null}

Is this normal?



via MikeVelazco

Advertisement