Search code examples
phplaravellaravel-5.3paginator

return Laravel Pagiation array item


I am collecting data from angular with below method.

$result = RepairCategory::select('id','Name')->paginate(10);

the result will be like this

enter image description here

I want to process the data array with some conditions. When I try to store the data in another variable.

$items = $result->data;

Its returning error like

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$data

I tried also

$items = $result['data'];

and

$items = $result[0]->data;

Please suggest a good method to get this...


Solution

  • You can access elements by DB column names such as id and Name. and

    You should write foreach loop to store all records in a array,

    $res = [];
    foreach($result as $row){
    $res[] = ['id'=>$row->id , 'Name' => $row->Name];
    }
    

    OR

    You can directly convert object to array by toArray() method.

    Reference links:-

    https://laravel.com/docs/5.3/pagination

    https://laravel.com/docs/5.3/eloquent-serialization#serializing-to-arrays