Quick Start


01. Installing Laravel and DataTables

composer create-project laravel/laravel datatables

cd datatables

composer require exist404/datatable-cruds

php artisan datatablecruds:install

02. Setup a Users DataTable

Open a new terminal in your datatables project directory and run the following command:

php artisan datatablecruds:for User

Next, we will configure our UserDataTableCruds and add the columns and inputs that we want to display.

app/Datatables/UserDataTableCruds.php

<?php

namespace App\Datatables;

use App\Models\User;
use Exist404\DatatableCruds\AbstractDatatable;
use Exist404\DatatableCruds\DatatableCruds;

class UserDatatableCruds extends AbstractDatatable
{
    public function init(): DatatableCruds
    {
        return $this->datatable->for(User::withCount("posts"))
            ->with('profile.phone')
            ->columns($this->columns())
            ->inputs($this->inputs())
            ;
    }

    protected function columns(): DatatableCruds
    {
        return  $this->datatable
            ->column("id")->sortable()->exportable()
            ->column('name')->sortable()->searchable()->exportable()
            ->column("email")->sortable()->exportable()
            ->column("created_at")->date()->sortable()->exportable()
            ->column("updated_at", function (User $row) {
                return $row->updated_at->diffForHumans();
            })->sortable()->exportable()
            ->column("select")->checkall()
            ->column("actions")->actions()
            ;
    }

    protected function inputs(): DatatableCruds
    {
        return  $this->datatable
            ->input("name")->type("text")
            ->input("email")->type("email")
            ->input("password")->type("password")
            ;
    }
}

03. Setup a Users Controller

php artisan make:controller UsersController

routes/web.php

use App\Http\Controllers\UsersController;

Route::apiResource('/users', UsersController::class);

app/Http/Controllers/UsersController.php

<?php

namespace App\Http\Controllers;

use App\Datatables\UserDatatableCruds;
use App\Models\User;
use Exist404\DatatableCruds\Facades\DatatableCruds;
use Illuminate\Http\Request;

class UsersController extends Controller
{
    public function index(UserDatatableCruds $datatable)
    {
        return $datatable->render();
    }

    public function store(Request $request)
    {
        User::create($request->all());
        return [
            'toast-message' => 'New User Has Been Added Successfully.',
            'toast-type' => 'success',
        ];
    }

    public function update(Request $request, $id)
    {
        User::where($request->findBy, $id)->first()->update($request->all());
        return [
            'toast-message' => 'User Has Been Updated Successfully.',
            'toast-type' => 'success',
        ];
    }

    public function destroy(Request $request, $id)
    {
        User::whereIn($request->findBy, explode(',', $id))->delete();
        return [
            'toast-message' => 'User Has Been Deleted Successfully.',
            'toast-type' => 'success',
        ];
    }
}

04. Migrate and Seed Test Data

php artisan migrate

php artisan tinker

Psy Shell v0.11.9 (PHP 8.1.6 — cli) by Justin Hileman
>>> User::factory(100)->create()               

License License License License License License