Laravel modifies pagination data

The pagination output of my Laravel is similar to a temporary laravel breakdown, but I need to change the data array for each object. My way out:

Output

As you can see, the data object has 2 elements that I need to change.

My code is:

$items = $this->items() ->where('position', '=', null) ->paginate(15); 

Which returns user elements with a pivot table, but I donโ€™t like the way the pivot table is shown in JSON, so I decided to change the elements and organize each element using a pivot in front of the element.

To this end, I tried to use foreach

 foreach ($items->data as $item) { } 

which gives me an error, for some reason I don't know:

 Undefined property: Illuminate\Pagination\LengthAwarePaginator::$data" status_code: 500 

Any help?

+14
source share
7 answers

Paginator elements is a collection. You can take it and convert the data as follows:

 $paginator = $this->items()->where('position', '=', null)->paginate(15); $paginator->getCollection()->transform(function ($value) { // Your code here return $value; }); 
+59
source

If you want the elements to be paginated:

  $itemsPaginated = $this->items() ->paginate(15); $itemsTransformed = $itemsPaginated ->getCollection() ->map(function($item) { return [ 'id' => $item->id, ]; })->toArray(); $itemsTransformedAndPaginated = new \Illuminate\Pagination\LengthAwarePaginator( $itemsTransformed, $itemsPaginated->total(), $itemsPaginated->perPage(), $itemsPaginated->currentPage(), [ 'path' => \Request::url(), 'query' => [ 'page' => $itemsPaginated->currentPage() ] ] ); 
+13
source

-Laravel 5.4

 // example update column "photo" // from "/path/to/photo.png" // to "abc.com/path/to/photo.png" foreach ($items as $item) { $path = $item->photo; // Remove $item->offsetUnset("photo"); // Set $item->offsetSet("photo", url($path)); } 
0
source

I had the same problem.
Then I discovered Reddish mutators .

They allow you to change your data when you receive or configure it.

In my example, I stored the HTML as Markdown, then when you extracted it into HTML.
You have included very little code in your question, so I will provide my version for your specific incident. In addition, the mutator code is placed in the model, and not in the controller.

 use Illuminate\Mail\Markdown; use Facades\League\HTMLToMarkdown\HtmlConverter as Markup; class Post extends Model { protected $fillable = [ 'title', 'body' ]; /** * Convert to HTML when displaying on the screen. */ public function getBodyAttribute($value) { return $this->attributes['body'] = Markdown::parse($value); } /** * Convert to Markdown when storing in the database. */ public function setBodyAttribute($value) { return $this->attributes['body'] = Markup::convert($value); } } 

Function names must meet the following criteria:

  • Start with get or set in lower case.
  • There should be your database column in PascalCase (or StudlyCase, since Laravel refers to it, although I believe it was incorrectly named. See fooobar.com/questions/500239 / ... here ).
  • Finally, the function name must end in Attribute with a capital A
0
source

In fact, $items is an instance of Paginator , which you can repeat like this:

 foreach ($items as $item) { } 

must work.

-one
source

you should use below code in your client

 {!! $items->render() !!} 
-4
source

Ignore pagination in laravel and get into normal data

 foreach ($items as $item) { } 
-4
source

All Articles