Reputation: 3121
With https://github.com/calebporzio/sushi I try to read data from external source and convert array with these data into Eloquent Paginator class.
I created a model class app/Models/Point.php :
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use \Sushi\Sushi;
// My PHPStorm greyed line above as non used - can it be that I need to use some trait or interface ?
class Point extends Model
{
use HasFactory;
protected $primaryKey = 'id';
protected $fillable = [
'title',
'cost',
'info',
];
protected $rows = [
];
public function getRows()
{
return $this->rows;
}
public function addRow(array $rowArray)
{
$this->rows[] = $rowArray;
}
}
public function __invoke($_, array $args)
{
$first = $args['first'] ?? 1;
$page = $args['page'] ?? 1;
$response = Http::get('https://externalapi.com/points');
$tempPoints = json_decode($response->getBody()->getContents());
$points = [];
$newPoint = new Point();
$row = 0;
foreach ($tempPoints as $tempPoint) {
$newPoint->addRow((array)$tempPoint);
}
// Just array written into log file
$points = $newPoint->getRows();
\Log::info(' -190 $points::');
\Log::info(json_encode($points));
$page = 1;
$first = 50;
$points = Point::offset(0)->limit(1000)->paginate($first, array('*'), 'page', $page);
\Log::info(' -191 $points::');
\Log::info(json_encode($points));
return $points;
}
In log file I see empty paginator:
[2023-04-25 13:54:46] local.INFO: {"current_page":1,"data":[],"first_page_url":"http://127.0.0.1:8000?page=1","from":null,"last_page":1,"last_page_url":"http://127.0.0.1:8000/?page=1","links":[{"url":null,"label":"« Previous","active":false},{"url":"http://127.0.0.1:8000/?page=1","label":"1","active":true},{"url":null,"label":"Next »","active":false}],"next_page_url":null,"path":"http://127.0.0.1:8000/","per_page":50,"prev_page_url":null,"to":null,"total":0}
Also tracing sql logs I see statement:
SELECT count(*) AS aggregate FROM `points`
I suppose that in my model has some invalid configurations and paginate method does not work properly and db is read...
FIXED BLOCK: I remade in app/Models/Point.php
class Point extends Model
{
use \Sushi\Sushi;
...
With that changes I do not see any sql-statements, but resulting pagination object has empty data. I tried 2 ways to get paginated data from Point mode:
$points = $newPoint->offset(0)->limit(1000)->paginate($first, array('*'), 'page', $page);
OR:
$points = Point::offset(0)->limit(1000)->paginate($first, array('*'), 'page', $page);
Which is valid way to get paginated data in this case ?
How to fix it ?
Upvotes: 0
Views: 852
Reputation: 11
Late, but maybe it helps someone else:
When sushi boots up, it is caching your rows property, that is empty on boot time.
You can just rename the rows:
protected $myRows = [
];
and use that in your getRows and addRow-Method or simply disable caching.
protected function sushiShouldCache() {
return false;
}
Upvotes: 0
Reputation: 1945
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use \Sushi\Sushi;
// My PHPStorm greyed line above as non used - can it be that I need to use some trait or interface ?
class Point extends Model
{
should be
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Factories\HasFactory;
class Point extends Model
{
use \Sushi\Sushi;
Upvotes: 0