Abdullah Çelik
Abdullah Çelik

Reputation: 1

Laravel 5 next and previous paging

Hello I have a list of max 5 articles in my homepage but I have many more articles and I would like to show these articles in other pages. So when I see more old posts, I would like to do a pagination like user home page / 1 when I get it as a result of posting it as url. But I do not follow the correct paths when I make directions. I need to change the following lines of code to help you change the point, good day, good day app/Http/Controller/HomeController:

public function deneme($page){
        $url = url()->full();
        $myUrl = explode('/', $url);
        $uz= sizeof($myUrl);
        $myUrl = $myUrl[$uz-1];
        if ($myUrl == 'work.com'){
            $yazilar = YaziModel::join('users as u','u.id','=', 'yazilar.kullaniciid')->select('yazilar.*','u.name','u.created_at')->orderBy('yazilar.id', 'DESC')->get();

            $posts = array_slice($yazilar->getIterator()->getArrayCopy(),0,5);
            return view('backend.pages.anasayfa')->with('yazilar', $posts);
        }else{
            $baslangic = $page*5;
            $yazilar = YaziModel::join('users as u','u.id','=', 'yazilar.kullaniciid')->select('yazilar.*','u.name','u.created_at')->orderBy('yazilar.id', 'DESC')->get();

            $posts = array_slice($yazilar->getIterator()->getArrayCopy(),$baslangic,5);
            return view('backend.pages.anasayfa')->with('yazilar' ,$posts);
        }

    }


routes/web.php:

Route::get('/{page}', 'HomeController@deneme');

View :

<ul class="pager">
                    <li class="next">
                        <a href="{{url()->full()}}">Older Post &rarr;</a>
                    </li>
                </ul>

Upvotes: 0

Views: 3027

Answers (2)

MD. Shafayatul Haque
MD. Shafayatul Haque

Reputation: 998

You can use this code in the blade (Laravel 5.7)

@if($news->previousPageUrl() != null)
    <a href="{{$news->previousPageUrl()}}" class="prev_btn pull-left"><i class="fa fa-chevron-left"></i> NEWER</a>
@endif

@if($news->nextPageUrl() != null)
    <a href="{{$news->nextPageUrl()}}" class="prev_btn pull-right">OLDER <i class="fa fa-chevron-right"></i> </a>
@endif

Upvotes: 3

Gowthaman D
Gowthaman D

Reputation: 579

Use Pagination

Controller

$users = DB::table('users')->paginate(15);

View

{{ $users->links() }}

for More Details https://laravel.com/docs/5.2/pagination

Upvotes: 1

Related Questions