david001's avatar

Call to a member function render() on array

I want to generate pagination links but i got error "Call to a member function render() on array"

my controller

    public function imageAlbum(){
      $albums = Album::orderBy('id','ASC')->where('type','=','0')->where('status', '=', '1' )->paginate(1);
      // dd($albums);
      if(count($albums) > 0):
        foreach ($albums as $value) {
          // dd($value->id);
          $imageAlbum['id'][]      = $value->id;
          $imageAlbum['title'][]   = $value->title;
          // $imageAlbum['type'][]    = $value->type;
          $imageAlbum['image'][]   = $value->image;
        }
        $imageAlbum['empty'] = 'false';
        array_push($imageAlbum, 'Image Gallery');
        array_push($imageAlbum, 'image');
        else:
          $imageAlbum['empty'] = 'true';
          array_push($imageAlbum, 'Image Gallery');
          array_push($imageAlbum, 'image');
        endif;
        return View('frontend.gallery.album')->with('albums',$imageAlbum);

      }

my view

```  {!!str_replace('/?','?',$albums->render())!!}```

but i got error like this

Call to a member function render() on array How can i generate pagination in this conditions

0 likes
3 replies
michaeldyrynda's avatar

You've converted your pagination instance to an array, which is why you can't access the render method anymore.

Consider moving that functionality which handles adding extra fields either into your Album model as accessors, or into a presenter class. Then handle the no results case within your view.

david001's avatar

That means i cannot use render() method for this situation

martinbean's avatar

@david001 What are you actually doing in that controller action? You seem to be doing a lot of work just to display some albums in a view. What’s wrong the something like the following?

$albums = Album::latest()->active()->ofType(0)->paginate();

return view('frontend.gallery.album', compact('albums'));

You then have a paginated list of albums that you can iterate over in your view:

@foreach ($albums as $album)
    <!-- Display album -->
@endforeach

<!-- Pagination -->
{!! $albums->render() !!}

In the controller action, you’ll see I re-factored your various WHERE clauses into more descriptive query scopes. I also don’t know what “0” refers to in the case of “type”, and the next person reading your code won’t either. Hell, even you might forget what type “0” is when you go back to the code after a period of time!

You want to reduce cognitive load when working with code, so that means getting rid of “magic” numbers that you have to pour through the codebase for to work out what they mean. Consider re-factoring that out into an enum-like class:

class AlbumType
{
    const PHOTOS = 0; // Or whatever ‘0’ represents in your application
}

This would make working with type values clearer:

$albums = Album::latest()->active()->ofType(AlbumType::PHOTOS)->paginate();
1 like

Please or to participate in this conversation.