3
好吧我想要在我的数据库表中不存在作为列的自定义字段。无法获取访问器方法中的属性 - laravel雄辩
我紧随其后,最后一部分: http://laravel.com/docs/4.2/eloquent#accessors-and-mutators
我的模型代码:
class Car extends Eloquent{
protected $fillable = array('driverID', 'fuelRemaining');
protected $appends = array('is_driver');
public function user(){
return $this->belongsTo('user');
}
public function getIsDriverAttribute(){
return ($this->attributes['driverID'] == Auth::user()->id);
}
}
车表:
Schema::create('cars', function(Blueprint $table)
{
$table->increments('id');
$table->integer('driverID');
$table->integer('fuelRemaining');
$table->mediumtext('desc');
$table->timestamps();
});
正如你可以看到我想要一个额外的领域,这是“is_driver “将被返回,但是当我运行这个时,这个字段用于通过比较ID来确定当前登录用户是否是驾驶员本人。
它将输出这个错误:
Undefined index: driverID
不知道我在做什么错在这里,请指教。
你能告诉我们你的汽车桌子是什么样子吗? – Jerodev
更新了问题。 – edward
奇怪的是我能够获得'$ this-> attributes ['id']' – edward