Laravel雄辩模特的临时财产 [英] Temporary property for Laravel Eloquent model

查看:100
本文介绍了Laravel雄辩模特的临时财产的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Laravel雄辩模型的用户,它有一个用户名和邮箱列表。我需要在运行时为模型添加一个属性,像$ user-> secure一样。这个属性不需要去数据库。



当我添加这个属性并点击$ user-> save()我得到一个错误,说我没有数据库列安全。在保存之前,我可以解决安全问题,但是不知何故,应该有更好的方法来做到这一点。任何建议?

解决方案

只需添加一个属性到您的课堂。

  class User Eloquent {
public $ secure;

// ...
}

请注意更好地声明它受保护,并添加相应的访问器和变异器( getSecure setSecure 方法)到你的模型。 p>

I have a Laravel Eloquent model User, which has a table with username and email columns. I need to add a property for the model on runtime, something like $user->secure. This property doesn't need to go to database.

When i add this property and hit $user->save() i get an error saying i don't have database column 'secure'. I can unset 'secure' before save but somehow it feels there should be a better way to do so. Any suggestions?

解决方案

Just add an attribute to your class.

class User extends Eloquent {
  public $secure;

  // ...
}

Note that it is better to declare it protected and add corresponding accessors and mutators (getSecure and setSecure methods) to your model.

这篇关于Laravel雄辩模特的临时财产的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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