Download Laravel App
composer create-project --prefer-dist laravel/laravel my-app
C:\xampp\htdocs\laravel10project>composer create-project laravel/laravel laravel10project
Connecting our Database
open .env file root directory.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laraveldb
DB_USERNAME=root
DB_PASSWORD=
Create Model and Migration
C:\xampp\htdocs\laravel\laravelproject>php artisan make:model Post -m
A new file named Post.php will be created in the app directory and database/migrations directory to generate the table in our database
app/Models/Post.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //app/Models/Post.php <?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; protected $fillable = [ 'title' , 'body' , 'slug' ]; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | //database\migrations\create_posts_table.php <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create( 'posts' , function (Blueprint $table ) { $table ->id(); $table ->string( 'title' ); $table ->string( 'slug' ); $table ->text( 'body' ); $table ->timestamps(); }); } public function down(): void { Schema::dropIfExists( 'posts' ); } }; |
php artisan migrate
C:\xampp\htdocs\laravel\laravel10project>php artisan migrate
Migration table created successfully.
check database table
Create Factory Class
php artisan make:factory PostFactory --model=Post
C:\xampp\htdocs\laravel\laravel10project>php artisan make:factory PostFactory --model=Post
database\factories\PostFactory.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | //database\factories\PostFactory.php <?php namespace Database\Factories; use Illuminate\Database\Eloquent\Factories\Factory; use App\Models\Post; use Illuminate\Support\Str; /** * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\Post> */ class PostFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = Post:: class ; /** * Define the model's default state. * * @return array */ public function definition() { return [ 'title' => $this ->faker->text(), 'slug' => Str::slug( $this ->faker->text()), 'body' => $this ->faker->paragraph() ]; } } |
php artisan tinker
C:\xampp\htdocs\laravel\laravel10project>php artisan tinker
App\Models\Post::factory()->count(50)->create();
Create Controller
C:\xampp\htdocs\laravel\laravel10project>php artisan make:controller PostController
app\Http\Controllers\PostController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | //app\Http\Controllers\PostController.php <?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; use Illuminate\View\View; class PostController extends Controller { public function index(Request $request ): View { $post = Post::paginate(3); if ( $request ->ajax()) { return view( 'data' , compact( 'post' )); } return view( 'post' ,compact( 'post' )); } } |
Bootstrap 5
https://getbootstrap.com/docs/5.0/getting-started/introduction/
https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css
Jquery
https://jquery.com/download/
CDN : jsDelivr CDN
https://www.jsdelivr.com/package/npm/jquery
https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js
resources/views/posts.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | //resources/views/posts.blade.php <!DOCTYPE html> <html> <head> <title>Laravel 10 Ajax Pagination</title> <link href= "https://cdn.jsdelivr.net/npm/bootstrap@5.0.2/dist/css/bootstrap.min.css" rel= "stylesheet" > </head> <body> <div class = "container" > <h2>Laravel 10 Ajax Pagination </h2> <div id= "data-wrapper" > <div class = "row" > @ include ( 'data' ) </div> </div> <div class = "d-flex" > {!! $post ->links() !!} </div> </div> <script type= "text/javascript" > $(window).on( 'hashchange' , function () { if (window.location.hash) { var page = window.location.hash.replace( '#' , '' ); if (page == Number.NaN || page <= 0) { return false; } else { getData(page); } } }); $(document).ready( function () { $(document).on( 'click' , '.pagination a' , function (event) { $( 'li' ).removeClass( 'active' ); $(this).parent( 'li' ).addClass( 'active' ); event.preventDefault(); var myurl = $(this).attr( 'href' ); var page=$(this).attr( 'href' ).split( 'page=' )[1]; getData(page); }); }); function getData(page){ $.ajax({ url: '?page=' + page, type: "get" , datatype: "html" , }) .done( function (data){ $( "#data-wrapper" ). empty ().html( "<div class='row'>" + data + "</div>" ); location.hash = page; }) .fail( function (jqXHR, ajaxOptions, thrownError){ alert( 'No response from server' ); }); } </script> </body> </html> |
1 2 3 4 5 6 7 8 9 10 11 12 | //resources/views/data.blade.php @ foreach ( $post as $rs ) <div class = "col-4" style= "padding-bottom:10px;" > <div class = "card" > <div class = "card-body" > <h5 class = "card-title" >{{ $rs ->id }} : {{ $rs ->title }}</h5> <p class = "card-text" >{!! $rs ->body !!}</p> </div> </div> </div> @ endforeach |
app/Providers/AppServiceProvider.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 | //app/Providers/AppServiceProvider.php <?php namespace App\Providers; use Illuminate\Pagination\Paginator; use Illuminate\Support\ServiceProvider; class AppServiceProvider extends ServiceProvider { /** * Register any application services. */ public function register(): void { // } /** * Bootstrap any application services. */ public function boot(): void { Paginator::useBootstrap(); } } |
1 2 3 4 5 6 7 8 9 10 11 | //routes/web.php <?php use Illuminate\Support\Facades\Route; use App\Http\Controllers\PostController; Route::get( '/' , function () { return view( 'welcome' ); }); Route::get( 'posts' ,[PostController:: class , 'index' ])->name( 'posts.index' ); |
Starting Laravel development server: http://127.0.0.1:8000/posts