dongyin8009
2018-06-19 10:02
浏览 343
已采纳

Laravel 5.6不在`created`模型事件中保存字段

I am trying to save a field from the created model event but for some reason the stripe_coupon_id column is never being saved. The created event does run as I've tested by trying a dd inside it and it does fire the event but just does not save that column.

class DiscountRate extends Model
{
    public $table = "discount_rates";

    public $primaryKey = "id";

    public $timestamps = true;

    public $fillable = [
        'id',
        'name',
        'rate',
        'active',
        'stripe_coupon_id'
    ];



    public static function boot()
        {
            parent::boot();
            self::created(function ($discountRate) {
                $coupon_id = str_slug($discountRate->name);
                $discountRate->stripe_coupon_id = $coupon_id;
            });

        }
}

In my controller I simply call a service function which calls the default Laravel model create function:

public function store(DiscountRateCreateRequest $request)
    {
        $result = $this->service->create($request->except('_token'));

        if ($result) {
            return redirect(route('discount_rates.edit', ['id' => $result->id]))->with('message', 'Successfully created');
        }

    }

discount_rates table:

enter image description here

图片转代码服务由CSDN问答提供 功能建议

我正在尝试从 created 模型事件中保存字段但由于某种原因 永远不会保存 stripe_coupon_id 列。 created 事件确实运行,因为我已经通过在其中尝试 dd 进行了测试,它确实触发了事件,但只是没有保存该列。

  class DiscountRate扩展Model 
 {
 public $ table =“discount_rates”; 
 
 public $ primaryKey =“id”; 
 
 public $ timestamps = true; 
 \  n public $ fillable = [
'id',
'name',
'rate',
'active',
'stripe_coupon_id'
 
]; 
 
 
 
 
公共静态函数 boot()
 {
 parent :: boot(); 
 self :: created(function($ discountRate){
 $ coupon_id = str_slug($ discountRate-> name); 
 $ discountRate->  stripe_coupon_id = $ coupon_id; 
}); 
 
} 
} 
   
 
 

在我的控制器中,我只需调用一个调用默认Laravel模型的服务函数 创建函数:

 公共函数存储(DiscountRateCreateRequest $ request)
 {
 $ result = $ this-> service-> cre  ate($ request-> except('_ token')); 
 
 if if($ result){
 return redirect(route('discount_rates.edit',['id'=>  $ result-> id])) - > with('message','Successfully created'); 
} 
 
} 
   
 
 

< code> discount_rates table:

  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 邀请回答

2条回答 默认 最新

相关推荐 更多相似问题