Laravel 4.2模型事件不会激发ajax

时间:2015-03-06 01:53:28

标签: php laravel

我在我的模特身上有这个

public static function boot()
{
    // there is some logic in this method, so don't forget this!
    parent::boot();

    Deal::created(function ($deal) {
        // or something
        $merchant = Deal::find($deal->deal_id)->merchant;
        $activity = new Activity;
        $activity->doer_id = $merchant->merchant_id;
        $activity->doer_name = $merchant->merchant_name;
        $activity->event_type= $merchant->merchant_name. " posted ".$deal->deal_name;
        $activity->save();


    });

    Deal::updated(function ($deal) {

                                    $merchant = Deal::find($deal->deal_id)->merchant;
                                $activity = new Activity;
                                $activity->doer_id = $merchant->merchant_id;
                                $activity->doer_name = $merchant->merchant_name;
                                if($$deal->deal_status == 1){
                                    $activity->event_type= $deal->deal_name. " was approved ";
                                }else{
                                    $activity->event_type= $deal->deal_name. " was rejected ";
                                }

                                $activity->save();


    });
}

正常的后期处理它保存在我的活动表上但是当我做ajax调用时我不会,它没有错误。请帮我解决这个问题

1 个答案:

答案 0 :(得分:0)

无论何时我需要在Laravel中使用ajax,我都会编写一个依赖于应用程序的API来使用Ajax。说实话,Laravel使用MVC风格的架构,遵循这些准则,只有控制器才能与模型交互。我的建议是写一个ajax调用发出请求的路由,并使用绑定到路由的控制器方法来处理ajax调用,并将信息保存到数据库中。