Install yajra laravel and use it

 Install yajra laravel and use it

yajra table


composer require yajra/laravel-datatables-oracle:"~9.0"

Open the file config/app.php and then add following service provider.

'providers' => [
    // ...
    Yajra\DataTables\DataTablesServiceProvider::class,
],
After completing the step above, use the following command to publish configuration & assets:
php artisan vendor:publish --tag=datatables

index.blade.php
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/css/bootstrap.min.css"
        integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/dataTables.bootstrap4.min.css">
    <link rel="stylesheet" href="https://cdn.datatables.net/1.12.1/css/jquery.dataTables.min.css">
    <title>data</title>
</head>

<body>

    <table id="mytable" class="table table-borderd">
        <thead>
            <tr>
                <th>id</th>
                <th>Name</th>
                <th>Price</th>
            </tr>
        </thead>
    </table>

    <script src="https://code.jquery.com/jquery-3.6.0.min.js"
        integrity="sha256-/xUj+3OJU5yExlq6GSYGSHk7tPXikynS7ogEvDej/m4=" crossorigin="anonymous"></script>
    <script src="https://cdn.datatables.net/1.12.1/js/jquery.dataTables.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@4.0.0/dist/js/bootstrap.min.js"
        integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous">
    </script>

    <script>
        $(document).ready(() => {
            $('#mytable').DataTable({
                processing: true,
                serverSide: true,
                ajax: '{!! route('product.loaddata') !!}',
                columns: [{
                        data: 'id',
                        name: 'id'
                    },
                    {
                        data: 'name',
                        name: 'name'
                    },
                    {
                        data: 'price',
                        name: 'price'
                    }
                ]

            });
        });
    </script>
</body>

</html>


web.php
<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProductsController;
/*
|--------------------------------------------------------------------------
| 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('/', function () {
    return view('welcome');
});

Route::get('/data',[ProductsController::class,'mydata'])->name('product.loaddata');
Route::get('/index',[ProductsController::class,'index'])->name('product.index');


ProductsController.php
<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\Products;
use Yajra\Datatables\Datatables;
class ProductsController extends Controller
{


 public function mydata()
 {
     return Datatables::of(Products::query())->make('true');
 }

 public function index()
 {
     return view('mydata');
 }

    }



Comments