skip如何在使用后和take查詢中獲得記錄總數?代碼工作:$records = Model::where('active', 1) ->skip(($page -1 ) * $max) ->take($max);// List of records$data = $records->get()//Total of all records including the skip records$total = $records->paginate()->total();我想要這種方式,但代碼不起作用:$records = Model::where('active', 1) ->skip(($page -1 ) * $max) ->take($max)->get();//Not Working $total = $records->paginate()->total();//Not Working $total = $records->total();//Not Working wrong result$total = $records->count();如何獲取集合中的所有總記錄?
3 回答

慕桂英546537
TA貢獻1848條經驗 獲得超10個贊
paginate()
直接在您的模型上使用該方法:
$records?=?Model::where('active',?1)->paginate($recordsPerPage);
這將返回一個LengthAwarePaginator
實例,它有很多有用的方法:
比如$records->total()
,$records->perPage()
等等

當年話下
TA貢獻1890條經驗 獲得超9個贊
嘗試這個
$records = Model::where('active', 1) ->skip(($page -1 ) * $max) ->take($max)->get(); $total = count($records);

梵蒂岡之花
TA貢獻1900條經驗 獲得超5個贊
在第一個示例中,您的呼叫 paginate() 是在查詢生成器上...(在您執行之前 ->get())
在您的第二個示例中,在您使用 ->get() 檢索結果后, paginate() 調用在集合中
$records 在兩者中都是不同的東西,首先是查詢構建器,其次是集合
- 3 回答
- 0 關注
- 189 瀏覽
添加回答
舉報
0/150
提交
取消