Hi Guys,
In this tutorial will learn How to load more data on a button click using JQuery in Laravel 10. I will tell you the best way to load more data on a button click using JQuery in Laravel 10. In this tutorial, we will load more data on button clicks using jQuery Laravel 10. We will use Laravel 10 on the button to load more data. In this tutorial, we will implement a Laravel 10 load more button Ajax. Let's see the below example to load more buttons in jQuery Laravel 10.
In this example, we will make a posts table using migration. Then, we will create a model for the posts and a factory class to create dummy data. After that, we will use JQuery AJAX to automatically load more data on a button click event and create a route to load posts.
We will complete this tutorial in below steps:
Step 1: Install Laravel
Step 2: Database Configuration
Step 3: Create Migration
Step 4: Create Model
Step 5: Create Factory Class
Step 6: Create Route
Step 7: Create Controller
Step 8: Create View File
Let's start with a step-by-step example.
Step 1: Install Laravel
composer create-project laravel/laravel example-app
Step 2 : Database Configuration
In this step, we require you to make a database configuration, you have to add the following details to your .env file.
DB_HOST=localhost
DB_DATABASE=homestead
DB_USERNAME=homestead
DB_PASSWORD=secret
Step 3: Create Migration
php artisan make:migration create_posts_table
After this command you will find one file in the following path "database/migrations" and you have to put the bellow code in your migration file to create a products table.
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->string('slug');
$table->text('body');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('posts');
}
};
Now you have to run this migration by following the command:
php artisan migrate
Step 4: Create Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
{
use HasFactory;
/**
* Write code on Method
*
* @return response()
*/
protected $fillable = [
'title', 'body', 'slug'
];
}
Step 5: Create Factory Class
php artisan make:factory PostFactory --model=Post
Further, put the below code in database\factories\PostFactory.php:
<?php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
use App\Models\Post;
use Illuminate\Support\Str;
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(): array
{
return [
'title' => $this->faker->text(),
'slug' => Str::slug($this->faker->text()),
'body' => $this->faker->paragraph()
];
}
}
Open the terminal, and execute the below commands to generate the test data:
php artisan tinker
Post::factory()->count(20)->create()
Step 6: Create Route
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('posts',[PostController::class,'index'])->name('posts.index');
Step 7: Create Controller
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index(Request $request)
{
$posts = Post::paginate(10);
if ($request->ajax()) {
$view = view('data', compact('posts'))->render();
return response()->json(['html' => $view]);
}
return view('posts', compact('posts'));
}
}
Step 8: Create View File
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta name="csrf-token" content="{{ csrf_token() }}">
<title>How to load more data on button click using JQuery in Laravel 10? - omcodingtutorial.com </title>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/css/bootstrap.min.css">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css" />
</head>
<body>
<div class="container mt-5" style="max-width: 750px">
<h1>How to load more data on button click using JQuery in Laravel 10? - omcodingtutorial.com</h1>
<div id="data-wrapper">
@include('data')
</div>
<div class="text-center">
<button class="btn btn-success load-more-data"><i class="fa fa-refresh"></i> Load More Data...</button>
</div>
<!-- Data Loader -->
<div class="auto-load text-center" style="display: none;">
<svg version="1.1" id="L9" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink"
x="0px" y="0px" height="60" viewBox="0 0 100 100" enable-background="new 0 0 0 0" xml:space="preserve">
<path fill="#000"
d="M73,50c0-12.7-10.3-23-23-23S27,37.3,27,50 M30.9,50c0-10.5,8.5-19.1,19.1-19.1S69.1,39.5,69.1,50">
<animateTransform attributeName="transform" attributeType="XML" type="rotate" dur="1s"
from="0 50 50" to="360 50 50" repeatCount="indefinite" />
</path>
</svg>
</div>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script>
var ENDPOINT = "{{ route('posts.index') }}";
var page = 1;
$(".load-more-data").click(function(){
page++;
infinteLoadMore(page);
});
function infinteLoadMore(page) {
$.ajax({
url: ENDPOINT + "?page=" + page,
datatype: "html",
type: "get",
beforeSend: function () {
$('.auto-load').show();
}
})
.done(function (response) {
if (response.html == '') {
$('.auto-load').html("Don't have more data to show");
return;
}
$('.auto-load').hide();
$("#data-wrapper").append(response.html);
})
.fail(function (jqXHR, ajaxOptions, thrownError) {
console.log('Server error occured');
});
}
</script>
</body>
</html>
@foreach ($posts as $post)
<div class="card mb-2">
<div class="card-body">{{ $post->id }}
<h5 class="card-title">{{ $post->title }}</h5>
{!! $post->body !!}
</div>
</div>
@endforeach
All the required steps have been completed, now you have to run the Laravel app with the help of this command:
php artisan serve
http://localhost:8000/posts
I hope it can help you guys...
0 Comments
Leave a Comment