Laravel 4.2选择雄辩的事件?

时间:2018-03-08 21:19:15

标签: php laravel laravel-4 eloquent

有没有办法在select / get上用eloquent添加一个事件?

我只看到这些方法:

public function getObservableEvents()
    {
        return array_merge(
            array(
                'creating', 'created', 'updating', 'updated',
                'deleting', 'deleted', 'saving', 'saved',
                'restoring', 'restored',
            ),
            $this->observables
        );
    }

我希望能够更改模型返回的值

我有这个型号:

class Fee extends Eloquent {
        protected $table = 'fees';
        protected $fillable = array('occ_paxs_id',
                                    'description',
                                    'amount',
                                    'other',
                                    'hstgst',
                                    'pst',);
    }

$fee = Fee::find($id); // here I want to be able to add a markup on the property amount

3 个答案:

答案 0 :(得分:1)

Eloquent支持自定义访问器和更改器。根据您问题中的模糊描述,这些可能会满足您的需求。

class Fee extends Eloquent
{
    protected $fillable = [
        'occ_paxs_id',
        'description',
        'amount',
        'other',
        'hstgst',
        'pst',
    ];

    protected $appends = [
        'formatted_amount',
    ];


    /**
     * Override the default attribute accessor.
     */
    public function getAmountAttribute($value)
    {
        return "This is the amount - {$value}";
    }

    /**
     * Or define a custom attribute accessor.
     */
    public function getFormattedAmountAttribute()
    {
        return "This is my formatted amount - {$this->amount}";
    }
}


$fee = Fee::find($id);

echo $fee->amount;  // This is the amount - 0.00
echo $fee->formatted_amount; // This is my formatted amount - 0.00

答案 1 :(得分:0)

你可以拥有Observers @ ORM(例如费用),但它只跟踪单个ORM实例的常规操作:创建,创建,更新,更新,删除,删除以及我相信的更多。

您可以使用这样的Mutator和Accessor在插入之前和检索数据之后更改数据:

class Fee extends Eloquent {
        protected $table = 'fees';
        protected $fillable = array('occ_paxs_id',
                                    'description',
                                    'amount',
                                    'other',
                                    'hstgst',
                                    'pst',);
        //Mutator = before it inserts into the database, it converts $value to a lower case string
        public function setAmountAttribute($value) {
             $this->attributes['first_name'] = strtolower($value);
        }

        //Accessor - after it retrieves data from the database, it places the capital letter in UpperCase (ucfirst).
        public function getAmountAttribute($value) {
              return ucfirst($value);
        }
}

通过创建以get开头并以Attribute结尾的函数,laravel根据名称查找相关属性。如果需要,您还可以获取不存在的属性并对其应用逻辑(例如,在下面的示例中获取一个人的fullName,这意味着将在您的ORM结果中返回名为fullName的变量及其内容):

public function getFullNameAttribute() {
    return $this->attributes['first_name'] " . " $this->attributes['last_name'];
}
  

https://laravel.com/docs/4.2/eloquent#accessors-and-mutators

答案 2 :(得分:0)

在 Laravel 5.5 中添加了一个 retrieved 事件。

<块引用>

当从数据库中检索现有模型时,将触发 retrieved 事件。

https://laravel.com/docs/5.5/eloquent#events

相关问题