ads

Laravel pagination with search

Step 1:Install Latest Laravel
composer create-project --prefer-dist laravel/laravel blog
Step 2:Setup database details into .env file
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=laravel
DB_USERNAME=root
DB_PASSWORD=
Step:3 create migration using below command
php artisan make:migration create_portfolios_table --create=portfolios

it will create a migration file into this folder - database/migration/

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePortfoliosTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('portfolios', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('name');
$table->longText('description');
$table->string('image');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('portfolios');
}
}
Step 4: migrate table
php artisane:migrate

this command only run successfully if you pass the perfect and truth values of env variables(you can check STEP 2) command

Step 5:Add Route
Route::resource('product','ProductController');
Step 6:Create Model
App/Product.php

here is used $fillable property which will check all time of ever operation time filled the column values
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
public $fillable=['name','image','price'];
}
Step 7:Create Controller using the command
php artisan make:controller ProductController --resource

we are using resource here it will create some specific methods into the controller like 

1)index
2)create
3)store
4)show
5)edit
6)update
7)destroy

Step 8:Create Controller
App/Http/Controllers/ProductController

replace below code as I defined here for app/Http/Controllers/ProductController.php file 
<?php
namespace App\Http\Controllers;

use App\Product;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Datatables;


class ProductController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$product = Product::paginate(10);
return view('product',compact('product'));
}

/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
//
}

/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
}

/**
* Display the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function show(Product $product)
{
//
}

/**
* Show the form for editing the specified resource.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
}

/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function update(Request $request,$id)
{
}

/**
* Remove the specified resource from storage.
*
* @param \App\Product $product
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{

}

}
Step 9:Create Resource


resources/views/product.blade.php

Here we need to put links() method in blade page so pagination will automatically show into that page
<!DOCTYPE html>
<html>
<head>
<title>Pagination Laravel Exmaple -<a href="https://www.mtitsolutions.in/">MTitsolutions</a></title>
<link rel="stylesheet" type="text/css" href="http://www.expertphp.in/css/bootstrap.css">
</head>
<body>
<div class="container">
<div class="table-responsive">
<table class="table table-bordered">
<thead>
<tr class="heading">
<th>No</th>
<th>Image</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
@foreach($product as $key => $val)
<tr>
<td>{{ ++$i }}</td>
<td><img class="{{$val->image}}" class="img-responsive" alt="product image"/></td>
<td>{!! $val->name !!}</td>
<td>{!! $val->price !!}</td>
</tr>
@endforeach
</tbody>
</table>
{!! $products->links() !!}
</div>
</div>
</body>
</html>
Now you can try to run on your computer and check the output laravel - pagination

If you wanna learn advance use of pagination than you can type code below
{!! $product->appends(['sort' => 'categoty'])->links() !!}
In a controller, you can use like this
$product = Product::paginate(10)->appends(['category' => category, 'sorting' => $sorting]);
you can write multiple values into appends also and For More information click here Laravel pagination with search ajax


Create custom pagination into laravel blade using below json array

{
"total": 50,
"per_page": 15,
"current_page": 1,
"last_page": 4,
"first_page_url": "http://laravel.app?page=1",
"last_page_url": "http://laravel.app?page=4",
"next_page_url": "http://laravel.app?page=2",
"prev_page_url": null,
"path": "http://laravel.app",
"from": 1,
"to": 15,
"data":[
{
// Result Object
},
{
// Result Object
}
]
}

Eg.

1)if you want to fetch the last available page number using the below code
$paginator->lastPage()

2)get the total number of page
$paginator->total()

3)get the current page number of page
$paginator->currentPage()

4)get the URL of the next page
$paginator->nextPageUrl()

5)get the URL of the previous page
$paginator->previousPageUrl()

6)get the items for the current page
$paginator->items()

I hope this code will help you  :)

Thank you.

1 Comments

  1. What is the best bet? - Make Money - Work-To-Earn
    Betting 메리트 카지노 주소 on sports is normally 코인카지노 코드 easy, with the titanium wire money you 온라인 카지노 can put your money หาเงินออนไลน์ back on or bet on. However, if you're a professional soccer player, make money on

    ReplyDelete

Post a Comment

Previous Post Next Post