Laravel Ajax滚动分页加载的实现

在进行laravel开发项目的时候,我们可能需要用到下拉滚动刷新的方式加载页面。那么在laravel中要怎么实现呢,下面介绍一个简单的方法:

控制器
namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests;
use App\Post;

class PostController extends Controller
{

    public function myPost(Request $request)
    {
        $posts = Post::paginate(6);  

        if ($request->ajax()) {
            $view = view('data',compact('posts'))->render();
            return response()->json(['html'=>$view]);
        }

        return view('my-post',compact('posts'));
    }

}
blade视图文件

resources/view/my-post.php




    Laravel 分页滚动加载
    
    
    



Laravel 分页滚动加载


@include('data')

resources/view/data.php

@foreach($posts as $post)

{{ $post->title }}

{{ str_limit($post->description, 400) }}


@endforeach

你可能感兴趣的:(Laravel Ajax滚动分页加载的实现)