duancong2160 2015-02-03 22:02
浏览 32

Laravel从自定义事件订阅者保存模型

When I try to Save a model from a EventHandler then the if( $event->save() ) always return false.

Any reason behind this? The $event is a user Model as I expect it to be.

<?php namespace AppName\Auth;

use Illuminate\Support\Facades\Hash;

class AuthEventHandler {

    public function onUserLogin($event) {
        $event->login_token = Hash::make('XXXX');
        if( $event->save() ) {
            dd(true);
        } else {
            dd(false);
        }
    }

    public function onUserLogout($event) {
        $event->login_token = null;
        $event->save();
    }

    public function subscribe($events) {
        $events->listen('auth.login', '\AppName\Auth\AuthEventHandler@onUserLogin');
        $events->listen('auth.logout', '\AppName\Auth\AuthEventHandler@onUserLogout');
    }
}
  • 写回答

1条回答 默认 最新

  • douzi1986 2015-02-03 22:53
    关注

    The problem was that I were using https://github.com/JeffreyWay/Laravel-Model-Validation as my extending class to the User object which made a validation cause error.

    评论

报告相同问题?