thinkphp5.0 使用paginate 分頁后 foreach 循環體內不能處理數據的解決辦法


方法一、使用toArray()將查詢出來的分頁數據轉成數組
$data = $goods_list->toArray();

方法二、 $data = $goods_list->all();

 

替換處理

<?php
//model里面的分頁函數
public function pageQuery(){    
    $rs = $this->where($where)->field(true)->order('id desc')->paginate()
    ->each(function($item, $key){
        if($item['status']==1){
            $item['name1'] = $item['name2'];
        }
        $item['imgSize'] = round($item['imgSize']/1024/1024,2);
        return $item;
    });
    return $rs;
}
?>

  

如果each里面涉及到別的表,則代碼如下:

<?php
//model里面的分頁函數
public function pageQuery(){    
    $rs = $this->where($where)->field(true)->order('id desc')->paginate()   
    ->each(function($item, $key){
        $urs = Db::name('u')->where('isShow',1)->select();
        foreach ($urs as $rkey=>$rv){
            if($item['userScore']>=$rv['startScore'] && $item['userScore']<$rv['endScore']){
               $item['userRank'] = $rv['rankName'];
            }
        }
        return $item;
    });
    return $rs;
}
?>

  

如果each里面涉及到外部參數,則代碼如下:

<?php
//model里面的分頁函數
public function pageQuery(){
//從別的表獲得參數值,一次獲取,each中可以重復使用
$urs = Db::name('u')->where('isShow',1)->select(); 
$rs = $this->where($where)->field(true)->order('id desc')->paginate() 
->each(function($item, $key) use ($urs){ 
//使用外部傳來的參數$urs
foreach ($urs as $rkey=>$rv){
if($item['userScore']>=$rv['startScore'] && $item['userScore']<$rv['endScore']){
$item['userRank'] = $rv['rankName'];
}
}
return $item;
});
return $rs;
}
?>

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM