如何检查Laravel的记录是否新的? [英] How to check if a record is new in Laravel?

查看:142
本文介绍了如何检查Laravel的记录是否新的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近开始使用雄辩。



当我使用PHP Active Record时,有一个很好的功能来检查是否从数据库加载了一个记录,或者是一个新的实例。有没有类似的雄辩,我可以使用?





新的我的意思是:

  $ article = new Article; 

而数据库中的一个将为

  $ article = Article :: find(1); 


解决方案

所有laravel模型都有一个 - >存在属性。



更具体地说,如果模型是从数据库加载的,或者自创建以来已经保存到数据库存在属性将为true;否则将是假的。



如果您想知道该模型是否已被修改,因为从数据库中被抓取,或者完全没有保存(也就是说,如果需要的话)保存),那么你可以使用 - > isDirty()函数。



Laravel API是一个有用的地方对于这种信息:
http://laravel.com/api/ 4.2 / Illuminate / Database / Eloquent / Model.html#method_isDirty
,通常比默认文档轻得多。


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 instance. Is there something similar in Eloquent that I could use?

By new I mean:

$article = new Article;

whereas one from the database would be

$article = Article::find(1);

解决方案

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.

这篇关于如何检查Laravel的记录是否新的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆