LemonTree 的博客

记录点滴,积少成多。


Laravel Eloquent 添加自定义字段


作者: 李茂琦

日期: 26 Sep 2016

在Laravel中使用Eloquent 时,我希望可以获取自定义的字段,这个字段在数据库中是不存在的,我可以这么做

在对应的模型(Modle)文件中,添加如下信息:

protected $appends = array('conf_elapse');
public function getConfElapseAttribute()
    {	
    	$starttime = $this->attributes['conf_runningtime'];
    	$curtime = date("Y-m-d H:i:s");    	
    	$elapse = strtotime($curtime)-strtotime($starttime);    	

    	//这里返回你想要给这个字段返回的值
    	return $elapse;    	
    }

这里有几点需要注意的:

  1. 添加字段,需要把字段名称添加到$appends
  2. 使用get函数获取属性值,get函数命名规则使用驼峰命名规则,首字母和下划线之后的字母需要大写,最后加上Attribute
  3. get函数中,可以使用$this->attributes['filed_name']获取指定字段filed_name的值