donglanzhan7151 2014-09-27 05:16
浏览 49
已采纳

在laravel中拯救雄辩的构造函数

So, I'm currently working on a browser game in Laravel. So far I love the framework, but I haven't really got much experience, and I just can't get this to work.

Basically I'm trying to update all users whenever they are instantieted, as there is no reason update them when they are not used. But calling this function from the constructor doesn't update the user, it only works when I call the function outside the constructor.

Have I missed anything, or is it just not possible?

Thanks in advance!

<?php
class User extends Eloquent implements UserInterface, RemindableInterface {

use UserTrait, RemindableTrait;

/**
 * The database table used by the model.
 *
 * @var string
 */
protected $table = 'users';

/**
 * The attributes excluded from the model's JSON form.
 *
 * @var array
 */
protected $hidden = array('password', 'remember_token');


public function __construct($arguments = array())
{
    parent::__construct($arguments);

    $this->updateHp();
}


public function updateHp()
{
    $this->hp_last = time();
    $this->save();
}   

}

展开全部

  • 写回答

2条回答 默认 最新

  • duanbei7005 2014-09-27 05:53
    关注

    Eloquent is a static class, data is fetched on query (find, first, get) and when you create a model you have just a blank model, with no data on it. This is, as example, the point where you have some data available:

    public static function find($id, $columns = array('*'))
    {
        if (is_array($id) && empty($id)) return new Collection;
    
        $instance = new static;
    
        return $instance->newQuery()->find($id, $columns);
    }
    

    Before one of those query methods, you have void.

    So you probably cannot do that during __construct because your model is still blank (all nulls). This is what you can do to make it, somehow, automatic:

    First, during boot, create some creating and updating listeners:

    public static function boot()
    {
        static::creating(function($user)
        {
            $user->updateHp($user);
        });
    
        static::updating(function($user)
        {
            $user->updateHp($user);
        });
    
        parent::boot();
    }
    
    public function updateHp()
    {
        $this->hp_last = time();
    
        $this->save();
    }
    

    Then, every time you save() a model it will, before saving, fire your method:

    $user = User::where('email', 'acr@antoniocarlosribeiro.com')->first();
    
    $user->activation_code = Uuid::uuid4();
    
    $user->save();
    

    If you want to make it somehow automatic for all your users. You can hook it to a login event. Add this code to your global.php file:

    Event::listen('user.logged.in', function($user) 
    {
       $user->updateHp();
    })
    

    Then in your login method you'll have to:

    if ($user = Auth::attempt($credentials))
    {
        Event::fire('user.logged.in', array($user)); 
    }
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部