composer create-project laravel/laravel datatables
cd datatables
composer require exist404/datatable-cruds
php artisan datatablecruds:install
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\DatatableCruds;
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Contracts\View\View;
class UserDataTableCruds
{
/**
* you can define all of your datatable globals methods here
*/
public static function init(): DatatableCruds
{
return datatableCruds()
// you can set your default values from config/datatablecruds.php file
// ->setBladeExtendsName('app') // define this value if your root layout blade file doesn't equal app
// ->setBladeSectionName('content') // define this value if your root layout blade doesn't have @yield('content')
->for(User::withCount("posts"))
->with('profile.phone')
->columns(static::columns())
->inputs(static::inputs())
;
}
/**
* you can define all of your datatable columns here
*/
protected static function columns(): DatatableCruds
{
return datatableCruds()
->column("id")->sortable()->exportable()
->column('name')->sortable()->searchable()->exportable()
->column("email")->sortable()->exportable()
->column("created_at")->sortable()->exportable()
->column("updated_at")->sortable()->exportable()
->column("select")->checkall()
->column("actions")->actions()
;
}
/**
* you can define all of your datatable forms inputs here
*/
protected static function inputs(): DatatableCruds
{
return datatableCruds()
->input("name")->type("text")
->input("email")->type("email")
->input("password")->type("password")
;
}
}
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()
{
return UserDatatableCruds::init()->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',
];
}
}
php artisan migrate
php artisan tinker
Psy Shell v0.11.9 (PHP 8.1.6 — cli) by Justin Hileman
>>> User::factory(100)->create()