Skip to content

Instantly share code, notes, and snippets.

@tobysteward
Last active January 2, 2025 07:28
Show Gist options
  • Save tobysteward/6163902 to your computer and use it in GitHub Desktop.
Save tobysteward/6163902 to your computer and use it in GitHub Desktop.

Revisions

  1. Toby Steward revised this gist Aug 12, 2013. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion BlogController.php
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,4 @@ public function showPosts()

    return View::make('blog', array('posts' => $posts));
    }
    }
    }
    2 changes: 1 addition & 1 deletion blog.blade.php
    Original file line number Diff line number Diff line change
    @@ -51,4 +51,4 @@ function getPosts(page) {
    </script>

    </body>
    </html>
    </html>
  2. Toby Steward revised this gist Aug 6, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion posts.blade.php
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    @foreach ($posts as $post)

    <article>
    <h2>{{ $post->title }} <a href="#{{ $post->id }}">#{{ $post->id }}</a></h2>
    <h2>{{ $post->title }}</h2>
    {{ $post->summary }}
    </article>

  3. Toby Steward revised this gist Aug 6, 2013. 4 changed files with 5 additions and 5 deletions.
    2 changes: 1 addition & 1 deletion BlogController.php
    Original file line number Diff line number Diff line change
    @@ -17,4 +17,4 @@ public function showPosts()

    return View::make('blog', array('posts' => $posts));
    }
    }
    }
    2 changes: 1 addition & 1 deletion Post.php
    Original file line number Diff line number Diff line change
    @@ -7,7 +7,7 @@ class Post extends Eloquent
    *
    * @var string
    */
    protected $table = 'blog_posts';
    protected $table = 'posts';

    /**
    * Define guarded columns
    4 changes: 2 additions & 2 deletions blog.blade.php
    Original file line number Diff line number Diff line change
    @@ -12,7 +12,7 @@
    <h1>Posts</h1>

    <div class="posts">
    @include('posts');
    @include('posts')
    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    @@ -51,4 +51,4 @@ function getPosts(page) {
    </script>

    </body>
    </html>
    </html>
    2 changes: 1 addition & 1 deletion posts.blade.php
    Original file line number Diff line number Diff line change
    @@ -7,4 +7,4 @@

    @endforeach

    {{ $posts->links() }}
    {{ $posts->links() }}
  4. Toby Steward revised this gist Aug 6, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions posts.blade.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    @foreach ($posts as $post)

    <article>
    <h2>{{ $post->title }} <a href="#{{ $post->id }}">#{{ $post->id }}</a></h2>
    {{ $post->summary }}
    </article>

    @endforeach

    {{ $posts->links() }}
  5. Toby Steward revised this gist Aug 6, 2013. 1 changed file with 15 additions and 23 deletions.
    38 changes: 15 additions & 23 deletions blog.blade.php
    Original file line number Diff line number Diff line change
    @@ -19,43 +19,35 @@
    <script>
    $(window).on('hashchange', function() {
    if (window.location.hash) {
    var page = window.location.hash.replace('#', '');
    if (page == Number.NaN || page <= 0) {
    return false;
    } else {
    getPosts(page);
    }
    $.ajax({
    url : '?page=' + page,
    dataType: 'json',
    }).done(function (data) {
    $('.posts').html(data);
    location.hash = page;
    }).fail(function () {
    alert('Posts could not be loaded.');
    });
    }
    });
    $(document).ready(function() {
    $(document).on('click', '.pagination a', function (e) {
    var page = $(this).attr('href').split('page=')[1];
    $.ajax({
    url : '?page=' + page,
    dataType: 'json',
    }).done(function (data) {
    $('.posts').html(data);
    location.hash = page;
    }).fail(function () {
    alert('Posts could not be loaded.');
    });
    getPosts($(this).attr('href').split('page=')[1]);
    e.preventDefault();
    });
    });
    function getPosts(page) {
    $.ajax({
    url : '?page=' + page,
    dataType: 'json',
    }).done(function (data) {
    $('.posts').html(data);
    location.hash = page;
    }).fail(function () {
    alert('Posts could not be loaded.');
    });
    }
    </script>

    </body>
  6. Toby Steward renamed this gist Aug 6, 2013. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  7. Toby Steward revised this gist Aug 6, 2013. 3 changed files with 81 additions and 1 deletion.
    2 changes: 1 addition & 1 deletion BlogController
    Original file line number Diff line number Diff line change
    @@ -15,6 +15,6 @@ class BlogController extends Controller
    return Response::json(View::make('posts', array('posts' => $posts))->render());
    }

    return View::make('test', array('posts' => $posts));
    return View::make('blog', array('posts' => $posts));
    }
    }
    18 changes: 18 additions & 0 deletions Post.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    <?php

    class Post extends Eloquent
    {
    /**
    * The database table used by the model.
    *
    * @var string
    */
    protected $table = 'blog_posts';

    /**
    * Define guarded columns
    *
    * @var array
    */
    protected $guarded = array('id');
    }
    62 changes: 62 additions & 0 deletions blog.blade.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,62 @@
    <!doctype html>
    <html lang="en">
    <head>

    <meta charset="utf-8">

    <title>Laravel AJAX Pagination with JQuery</title>

    </head>
    <body>

    <h1>Posts</h1>

    <div class="posts">
    @include('posts');
    </div>

    <script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
    <script>
    $(window).on('hashchange', function() {
    if (window.location.hash) {
    var page = window.location.hash.replace('#', '');
    if (page == Number.NaN || page <= 0) {
    return false;
    }
    $.ajax({
    url : '?page=' + page,
    dataType: 'json',
    }).done(function (data) {
    $('.posts').html(data);
    location.hash = page;
    }).fail(function () {
    alert('Posts could not be loaded.');
    });
    }
    });
    $(document).ready(function() {
    $(document).on('click', '.pagination a', function (e) {
    var page = $(this).attr('href').split('page=')[1];
    $.ajax({
    url : '?page=' + page,
    dataType: 'json',
    }).done(function (data) {
    $('.posts').html(data);
    location.hash = page;
    }).fail(function () {
    alert('Posts could not be loaded.');
    });
    e.preventDefault();
    });
    });
    </script>

    </body>
    </html>
  8. Toby Steward created this gist Aug 6, 2013.
    20 changes: 20 additions & 0 deletions BlogController
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    <?php

    class BlogController extends Controller
    {
    /**
    * Posts
    *
    * @return void
    */
    public function showPosts()
    {
    $posts = Post::paginate(5);

    if (Request::ajax()) {
    return Response::json(View::make('posts', array('posts' => $posts))->render());
    }

    return View::make('test', array('posts' => $posts));
    }
    }