分页回调处理each方法有错误

浏览:3757 发布日期:2017/09/04 分类:ThinkPHP5专区
$list = User::where('status',1)->paginate()->each(function($item, $key){
    $item['nickname'] = '11';
});
官方介绍方法,无法把处理后的数据返回$list.
查看TP5,each方法代码:/**
     * 给每个元素执行个回调
     *
     * @param  callable $callback
     * @return $this
     */
    public function each(callable $callback)
    {
        foreach ($this->items as $key => $item) {
              if ($callback($item, $key) === false) {
                break;
            }
        }        
        return $this;
    }
foreach 内对$this值 没有影响;
修改代码:public function each(callable $callback)
    {
        foreach ($this->items as $key => $item) {
            $this->items[$key] = $callback($item, $key);
            if ($callback($item, $key) === false) {
                break;
            }
        }
        
        return $this;
    }
前端:$list = User::where('status',1)->paginate()->each(function($item, $key){
    $item['nickname'] = '11';
     return $item;
});
可以正常赋值,这个是否是官方的bug?
最佳答案
评论() 相关
后面还有条评论,点击查看>>