I get information from the web service but this error is given when I try to do the pagination I saw other questions but they all took information from the database and did not help my problem.
Call to a member function paginate() on array (View: /media/mojtaba/Work/Project/bit/resources/views/backend/crypto/cryptolist.blade.php)
and my code:
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);
}
}
You should use Collection
for this, but collection has no paginate
method but we can use macros
to extend
it
Open AppServiceProvider.php
and paste this on boot
method
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,
]
);
});
also import
this incase
use Illuminate\Support\Collection;
use Illuminate\Pagination\LengthAwarePaginator;
then in your render
method you can, use collect([...])->paginate(10)
just like below
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 for extending paginate
method with Collection
using macros
.