1

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?

MikeVelazco
  • 885
  • 2
  • 13
  • 30

2 Answers2

1

Yes that is normal. Eloquent doesn't save and then do another query to select all the data for the record it just inserted.

lagbox
  • 48,571
  • 8
  • 72
  • 83
  • I can save and then find by ID, but isn't there a way to do it in one step provided by eloquent? – MikeVelazco Mar 15 '17 at 18:51
  • You can call `->fresh()` and it should load the data from database. `... $oName->save(); $oName->fresh();` – DevK Mar 15 '17 at 18:53
  • Better yet, since your column `Date` is pretty much same as `created_at` you could tell Laravel to look for `created_at` in column `Date` - [like this](http://stackoverflow.com/questions/25010435/laravel-custom-timestamp-column-names). That way Laravel will attach a timestamp to Date by default when a new model is saved and you won't need to do an additional query to get this result. – DevK Mar 15 '17 at 18:57
  • I agree with @devk, but it seems that author doesn't requre `updated_at` field of some analog of this field. There is no proper way to disable one of the automatic "timestamps" field in Laravel, only by using `boot` method of the model and setting desired parameter manually (as I wrote in my answer). In this case there is no need to say, that you should `const CREATED_AT = 'date';` in your model to re-define default field name for creation date `created_at`. – Alexander Reznikov Mar 15 '17 at 19:11
  • 1
    @devk You're right, but you must reset or return the `$oName->fresh()`, because it doesn't update the model attributes, so what I did is `$oName = $oName->fresh()`, there's an answer related [here](http://stackoverflow.com/a/27748794/3541922) – MikeVelazco Mar 15 '17 at 19:29
1

You can manually set field date, just add this code into your mode:

public static function boot()
{
    static::creating(function ($model) {
        $model->date = date('Y-m-d H:i:s');
    });
}

You will avoid second DB request for selecting inserted item. Just be carefull with timezones when you're using date function.

Also you can use getAttributes() Eloquent method and get rid of stdClass:

function save($sName)
{
    $oName = new Names();
    $oName->Name = $sName;
    $oName->save();
    return $oName->getAttributes();
}
Alexander Reznikov
  • 1,266
  • 1
  • 14
  • 23