我如何挂钩到模型的返回值->;all()或Model->;Laravel 4中的get()


How can I hook into the return values of either Model->all() or Model->get() in Laravel 4?

假设您有以下模型:

// Model - Very basic
class VenueType extends Eloquent {
    protected $table = "venue_types";        
}
// Database table
int id,
varchar(255) name,
varchar(255) address

在控制器上,我会运行这样的程序:

// Controller
$results = VenueType::all();

在模型中,是否有过滤/挂钩(我不太愿意说过滤,因为它在Laravel中有意义)值。例如,将标题大小写函数添加到地址(ucwords)。

// Model
public function hookAddress(value) {
    return ucwords(value);
}  

使用Eloquent访问者

class VenueType extends Eloquent {
protected $table = "venue_types";
    public function getAddressAttribute($value)
    {
        return ucwords($value);
    }
}