donglanzhan7151 2014-09-27 13: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 13: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条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!