2014-12-19 54 views
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 

不知道我在做什么错在这里,请指教。

+0

你能告诉我们你的汽车桌子是什么样子吗? – Jerodev

+0

更新了问题。 – edward

+0

奇怪的是我能够获得'$ this-> attributes ['id']' – edward

回答

5

啊我找到了原因。这是未来的读者

在我的控制器的引用当我加入的AuthorID的get阵列

$car = Car::where('fuelRemaining', 0)->get(array('id', 'desc', 'authorID')); 

我能够得到的AuthorID属性,我只得到这两个

$car = Car::where('fuelRemaining', 0)->get(array('id', 'desc')); 

我的自定义访问者在问题中提到。