在变量中使用时调用数组的成员函数 paginate()
Call to a member function paginate() on array when use in variable
我从网络服务中获取信息,但是当我尝试进行分页时出现此错误
我看到了其他问题,但他们都是从数据库中获取信息,对我的问题没有帮助。
Call to a member function paginate() on array (View: /media/mojtaba/Work/Project/bit/resources/views/backend/crypto/cryptolist.blade.php)
和我的代码:
public function render()
{
try {
$api = new \Binance\API('api','secret');
$prices = $api->coins();
$one = json_encode($prices, true);
$coins = json_decode($one , true);
return view('livewire.backend.crypto.cryptolist')->with('coins' , $coins->paginate(10));
}catch(\Exception $e)
{
return view('wrong')->with('e' , $e);
}
}
你应该使用Collection
,但是集合没有paginate
方法,但我们可以使用macros
到extend
它
打开 AppServiceProvider.php
并将其粘贴到 boot
方法中
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
还有import
这种情况
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
然后在您的 render
方法中,您可以像下面那样使用 collect([...])->paginate(10)
public function render() {
try {
$api = new \Binance\API('api','secret');
$coins = $api->coins();
return view('livewire.backend.crypto.cryptolist')->with('coins',collect($coins)->paginate(10));
} catch(\Exception $e) {
return view('wrong')->with('e' , $e);
}
}
Reference 使用 macros
.
扩展 paginate
方法 Collection
我从网络服务中获取信息,但是当我尝试进行分页时出现此错误 我看到了其他问题,但他们都是从数据库中获取信息,对我的问题没有帮助。
Call to a member function paginate() on array (View: /media/mojtaba/Work/Project/bit/resources/views/backend/crypto/cryptolist.blade.php)
和我的代码:
public function render()
{
try {
$api = new \Binance\API('api','secret');
$prices = $api->coins();
$one = json_encode($prices, true);
$coins = json_decode($one , true);
return view('livewire.backend.crypto.cryptolist')->with('coins' , $coins->paginate(10));
}catch(\Exception $e)
{
return view('wrong')->with('e' , $e);
}
}
你应该使用Collection
,但是集合没有paginate
方法,但我们可以使用macros
到extend
它
打开 AppServiceProvider.php
并将其粘贴到 boot
方法中
Collection::macro('paginate', function($perPage, $total = null, $page = null, $pageName = 'page') {
$page = $page ?: LengthAwarePaginator::resolveCurrentPage($pageName);
return new LengthAwarePaginator(
$this->forPage($page, $perPage),
$total ?: $this->count(),
$perPage,
$page,
[
'path' => LengthAwarePaginator::resolveCurrentPath(),
'pageName' => $pageName,
]
);
});
还有import
这种情况
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
然后在您的 render
方法中,您可以像下面那样使用 collect([...])->paginate(10)
public function render() {
try {
$api = new \Binance\API('api','secret');
$coins = $api->coins();
return view('livewire.backend.crypto.cryptolist')->with('coins',collect($coins)->paginate(10));
} catch(\Exception $e) {
return view('wrong')->with('e' , $e);
}
}
Reference 使用 macros
.
paginate
方法 Collection