PHP code example of gustocoder / laravel-datatable
1. Go to this page and download the library: Download gustocoder/laravel-datatable library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gustocoder / laravel-datatable example snippets
namespace GustoCoder\LaravelDatatable\Http\Controllers;
use App\Http\Controllers\Controller;
use Gustocoder\LaravelDatatable\Http\Controllers\DatatableController;
class ExampleController extends Controller
{
public function showUsers() {
$dataTableClass = new DatatableController(
'User',
'users',
[],
['date_field' => 'created_at', 'orderBy' => 'created_at']
);
//give the full route path (NOT the route name) as defined in the route file, eg 'admin/users'
$deleteRoute = 'deleteUser';
$editRoute = 'editUsers';
//This is optional, & it creates a new column, eg 'Actions'. It accepts the name of the column
//and you can call it whatever you want. It only supports adding one column, so only call this
//once. You can however, add multiple field buttons under that columns, and the column will be
//expanded to contain them all eg Actions.
//If you do call addColumn, make sure you also call addFieldButton(...) to insert data under
//the new column
$dataTableClass->addColumn('Action');
//used to add field data to go under the column you added above. Handy for Edit, or Delete buttons.
$dataTableClass->addFieldButton(
'Action',
'Delete',
'x',
$deleteRoute,
['id'],
['id' => 'deleteUserBtn', 'class' => 'btn btn-danger btn-sm']
);
$dataTableClass->addFieldButton(
'Action',
'Edit',
'Edit',
$editRoute,
['id'],
['id' => 'editUserBtn', 'class' => 'btn btn-warning btn-sm']
);
//add another button if you want. Give it relevant attributes
$dataTableClass->addFieldButton(
'Action',
'Something',
'something',
'someRoute',
['id'],
['id' => 'doSomethingBtn', 'class' => 'btn btn-primary btn-sm']
);
$panelId = 'usersPanel';
$usersTable = $dataTableClass->getTable($panelId);
return view('laravel-datatable::datatable-view', ['usersTable' => $usersTable]);
}
/**
* An example of how you would delete a record from the datatable-see the delete
* button link and the route that points to this method
*/
public function deleteUser($userId)
{
$userModel = new User();
$record = $userModel::find($userId);
if ($record) {
$record->delete();
return redirect()->back()->with('success', 'User deleted successfully');
}
else
{
return redirect()->back()->with('danger', 'User could not be deleted');
}
}
}
use Illuminate\Support\Facades\Route;
use Gustocoder\LaravelDatatable\Http\Controllers\ExampleController;
//This is an example of how you would define routes for the feature
Route::get('/users', [ExampleController::class, 'showUsers'])->name('show-users');
Route::get(
'/deleteUser/{userId}',
[ExampleController::class, 'deleteUser']
)->name('delete-user');