TP5.0分頁回調 修改數據

分頁回調後修改某個字段的值each失效問題

 $cat = Db::table('g_user')->alias(['g_user'=>'user','g_department'=>'department'])->join("department","user.parentid= department.id")->where($where_depart)->paginate($pagesize,false,['query'=>request()->param()])->each(function($item,$key){
            $item['name'] = $item['name'].'/';
            return $item;
        });

發現失效
each方法在library/think/collection.php
在這裏插入圖片描述
這是更正後的each方法

    /**
     * 給每個元素執行個回調 方法更正 2020/4/26
     * @param  callable $callback
     * @return $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;

    }

備註
應該是 tp5.1之後each()便是修正了的,5.1以上的自動忽略第一步,如果修改或者增加失敗的,請檢查each()
另外
pagenate 第二個參數 true 產生的分頁樣式 是上下的
在這裏插入圖片描述
這樣操作下來分頁調用 ->render()
報錯:調用未定義的方法think \ Collection :: render()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章