In this tutorial, we will create laravel 8 livewire datatables with Tailwind CSS. For this section we will use Laravel 8, rappasoft laravel-livewire-tables package and laravel breeze starter kit, laravel breeze come with tailwind css and alpine js, and it is simple and clean user interface.
Laravel Livewire Datatables With Tailwind CSS Example
Step 1: Set Up Laravel Project
Step 2: Set Up Database Details in ENV
Step 3: Install breeze & Setup Livewire
Step 4: Set up Datatable with laravel livewire & Tailwind CSS
Step 1: Set Up Laravel Project
Installing a fresh new laravel application, so head over to the terminal, type the command, and create a new laravel app.
composer create-project --prefer-dist laravel/laravel larainfo
Step 2: Set Up Database Details in ENV
Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.
.env
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password
Step 3: Install breeze & Setup Livewire
Install breeze
You need to install first breeze package
composer require laravel/breeze --dev
Then, install breeze ui
php artisan breeze:install
Install livewire
composer require livewire/livewire
Include the JavaScript (on every page that will be using Livewire).
...
@livewireStyles
</head>
<body>
...
@livewireScripts
</body>
</html>
After, you need to install dependencies
npm install && npm run dev
And final run migrate
php artisan migrate
Step 4: Set up Datatable with laravel livewire & Tailwind CSS
Install Rappasoft laravel-livewire-tables
composer require rappasoft/laravel-livewire-tables
publish the config file
php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider" --tag=livewire-tables-config
php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider" --tag=livewire-tables-views
php artisan vendor:publish --provider="Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider" --tag=livewire-tables-translations
Configure for datatables
app/config/livewire-tables.php
For Tailwind
<?php
return [
/**
* Options: tailwind | bootstrap-4 | bootstrap-5.
*/
'theme' => 'tailwind',
];
Create Fake Data With Tinker
php artisan ti
create 30 fake user using tinker factory.
Psy Shell v0.10.8 (PHP 7.4.22 — cli) by Justin Hileman >>> App\Models\User::factory(30)->create();
Create User Controller
php artisan make:controller UserController
app/Http/Controllers/UserController.php
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class UserController extends Controller
{
public function index()
{
$users = User::all();
return view('user', compact('users'));
}
}
Create user.blade.php file
app/resources/views/user.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<!-- Datatables will be here -->
</div>
</div>
</div>
</div>
</x-app-layout>
Create route web.php
<?php
use App\Http\Controllers\UserController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| 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('/users', [UserController::class, 'index']);
Route::get('/dashboard', function () {
return view('dashboard');
})->middleware(['auth'])->name('dashboard');
require __DIR__ . '/auth.php';
Create Livewire component
php artisan make:livewire UsersTable
COMPONENT CREATED 🤙
CLASS: app/Http/Livewire/UsersTable.php
VIEW: resources/views/livewire/users-table.blade.php
Implements The Rappasoft laravel livewire tables Code
app/Http/Livewire/UsersTable.php
<?php
namespace App\Http\Livewire;
use App\Models\User;
use Livewire\Component;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Views\Column;
class UsersTable extends DataTableComponent
{
public function columns(): array
{
return [
Column::make('Name')
->sortable()
->searchable(),
Column::make('E-mail', 'email')
->sortable()
->searchable(),
Column::make('Created At', 'created_at')
->sortable()
->searchable(),
Column::make('Updated At', 'updated_at')
->sortable()
->searchable(),
];
}
public function query(): Builder
{
return User::query();
}
}
Add <livewire:users-table /> file location
app/resources/views/user.blade.php
<x-app-layout>
<x-slot name="header">
<h2 class="font-semibold text-xl text-gray-800 leading-tight">
{{ __('Dashboard') }}
</h2>
</x-slot>
<div class="py-12">
<div class="max-w-7xl mx-auto sm:px-6 lg:px-8">
<div class="bg-white overflow-hidden shadow-sm sm:rounded-lg">
<div class="p-6 bg-white border-b border-gray-200">
<livewire:users-table />
</div>
</div>
</div>
</div>
</x-app-layout>
And final run server
php artisan serve
http://localhost:8000/