栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > PHP

Laravel 简单实现Ajax滚动加载示例

PHP 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Laravel 简单实现Ajax滚动加载示例

开发H5项目的时候我们总是需要用到下拉滚动刷新的方式加载页面。这里用 Laravel 实现一下,直接上代码:

创建模型

这里我们不妨创建一个 文章(Post)模型, 并且生成测试数据 50 条吧。

php artisan make:model -m

模型Post.php

namespace App;

use IlluminateDatabaseEloquentModel;

class Post extends Model
{

 public $fillable = ['title','description'];

 
}

迁移文件

use IlluminateDatabaseSchemaBlueprint;
use IlluminateDatabaseMigrationsMigration;

class CreatePostTable extends Migration
{
 
 public function up()
 {
  Schema::create('posts', function (Blueprint $table) {
   $table->increments('id');
   $table->string('title');
   $table->text('description');
   $table->timestamps();
  });
 }

 
 public function down()
 {
  Schema::drop("posts");
 }
}

测试数据 ModelFactory.php

$factory->define(AppPost::class, function (FakerGenerator $faker) {
 return [
  'title' => $faker->sentence,
  'description' => $faker->paragraph,
 ];
});

填充

call(UsersTableSeeder::class);
  factory(AppPost::class, 50)->create();
 }
}

路由

Route::get('my-post', 'PostController@myPost');

控制器

namespace AppHttpControllers;

use IlluminateHttpRequest;
use AppHttpRequests;
use AppPost;

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'));
 }

}

视图文件 resources/view/my-post.php




 Laravel 分页滚动加载
 
 
 
  .ajax-load{
   background: #e1e1e1;
   padding: 10px 0px;
   width: 100%;
  }
 




 

Laravel 分页滚动加载
@include('data')

![](./loader.gif)加载更多……

resources/view/data.php

@foreach($posts as $post)

 {{ $post->title }}
 

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


@endforeach

效果:

以上这篇Laravel 简单实现Ajax滚动加载示例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持考高分网。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/38775.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号