anon160124's avatar

Pagination album images

Hello, can anyone advise me how to do pagination in the model? I have an album with pictures and I would like to see, for example, 20 pictures. Thanks for help

namespace App;

use Illuminate\Database\Eloquent\Model;

class Album extends Model {

protected $fillable =
[
    'title', 'text', 'image'
];
public function images()
{
  return $this->hasMany('App\Image')->latest();
}

}

0 likes
5 replies
MichalOravec's avatar

@martin1182 Something like this

namespace App\Http\Controllers;

use App\Album;
use App\Http\Controllers\Controller;

class AlbumController extends Controller
{
    public function show(Album $album)
    {
        $images = $album->images()->paginate(20);

        return view('album.show', compact('album', 'images'));
    }
}
MichalOravec's avatar

@martin1182 Could you post your whole code? Because this is definitely good.

$images = $album->images()->paginate(20);
Snapey's avatar
Snapey
Best Answer
Level 122

how to do pagination in the model?

You cannot, at least not within a relation function.

Please or to participate in this conversation.