【问题标题】:Laravel 4.2 Eloquent event on select?Laravel 4.2 Eloquent 事件选择?
【发布时间】:2018-03-08 21:19:14
【问题描述】:

有没有办法通过 eloquent 在 select/get 上添加事件?

我只看到这些方法:

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

【问题讨论】:

    标签: php laravel laravel-4 eloquent


    【解决方案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
    

    【讨论】:

      【解决方案2】:

      您可以拥有观察者 @ 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 根据名称查找相关的属性。如果需要,您还可以获取不存在的属性并对其应用逻辑(例如,在下面的示例中获取一个人的全名,这意味着将在您的 ORM 结果中返回一个名为全名的变量及其内容):

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

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

      【讨论】:

        【解决方案3】:

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

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

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

        【讨论】:

          猜你喜欢
          • 2015-08-19
          • 2013-06-10
          • 1970-01-01
          • 2015-02-23
          • 1970-01-01
          • 1970-01-01
          • 2014-02-10
          • 2018-01-23
          • 1970-01-01
          相关资源
          最近更新 更多