How to check if a record is new in Laravel?

后端 未结 5 1887
星月不相逢
星月不相逢 2020-12-30 19:11

I recently started using Eloquent.

When I used PHP Active Record, there was a nice function that checked if a record was loaded from the database or is a new instan

5条回答
  •  情深已故
    2020-12-30 19:37

    All laravel models have a ->exists property.

    More specifically if the model is either loaded from the database, or has been saved to the database since being created the exists property will be true; Otherwise it will be false.

    If you wish to know if the model has been modified since being grabbed from the database, or simply not saved at all (aka if it needs saving) then you can use the ->isDirty() function.

    The Laravel API is a useful place for this kind of information: http://laravel.com/api/4.2/Illuminate/Database/Eloquent/Model.html#method_isDirty and often sheds far more light than the default documentation.

提交回复
热议问题