关于在l5中使用分页进行无限滚动,我找到了很多文章,但它们都使用了这个分页()函数,因为它们使用的是db中的结果集,但我是以json的身份从googlefontapi中获取数据的,所以当我在json中使用分页()时,它会导致一个错误,也会导致数组中的错误。我的代码
public function index(){
$url = "https://www.googleapis.com/webfonts/v1/webfonts?key=!";
$result = json_decode(file_get_contents( $url ))->paginate(10);
$font_list = "";
foreach ( $result->items as $font )
{
$font_list[] = [
'font_name' => $font->family,
'category' => $font->category,
'variants' => implode(', ', $font->variants),
// subsets
// version
// files
];
}
return view('website_settings')->with('data', $font_list);
}错误是
Call to undefined method stdClass::paginate()有没有其他方法可以做到这一点?
发布于 2016-06-22 14:46:20
对于您的情况,您需要使用Illluminate\Support\Collection。然后,我们可以将Illuminate\Support\Collection传递给Illuminate\Pagination\Paginator类的一个实例,以获取Illuminate\Pagination\Paginator实例。请确保使用use Illuminate\Pagination\Paginator。
use Illuminate\Pagination\Paginator;然后,根据您的结果创建一个集合:
$collection = collect(json_decode($file_get_contents($url), true));最后,构造分页器。
$paginator = new Paginator($collection, $per_page, $current_page);或者一行,因为这就是你滚动的方式:
$paginator = new Paginator(collect(json_decode($file_get_contents($url), true)));还可以在需要时缓存集合,只有在请求不是XHR请求时才重新加载它,例如在页面加载期间。当您需要将API请求保持在最低限度时,这很有用,而且通常还有助于提高请求的性能,因为任何HTTP请求都会有与之相关的延迟。
希望这能有所帮助。
https://stackoverflow.com/questions/37960283
复制相似问题