Downloads
Stars
Version
Laravel Table allows you to easily render tables in your views, directly built from PHP code.
This package is shipped with a pre-configuration for Bootstrap 4.*
and FontAwesome 5
.
However, the templates customization makes it very simple to use with another UI framework.
Laravel version | PHP version | Package version |
---|---|---|
^5.5 | ^7.1 | ^1.0 |
Prepare the table configuration somewhere in your code and pass it to the view :
$table = (new \Okipa\LaravelTable\Table)->model(\App\News::class)->routes([
'index' => ['name' => 'users.index'],
'create' => ['name' => 'user.create'],
'edit' => ['name' => 'user.edit'],
'destroy' => ['name' => 'user.destroy'],
])->destroyConfirmationHtmlAttributes(function (User $user) {
return [
'data-confirm' => 'Are you sure you want to delete the user ' . $user->name . ' ?',
];
});
$table->column('first_name')->sortable(true)->searchable();
$table->column('last_name')->sortable()->searchable();
$table->column('email')->sortable()->searchable();
In your view, simply render your table like this :
{{ $table }}
composer require "evgeniy-silantev/laravel-table:^1.0"
Publish the package configuration and override the available config values :
php artisan vendor:publish --tag=laravel-table:config
To customize the existing translations, publish the packages translations files to make the wanted changes :
php artisan vendor:publish --tag=laravel-table:translations
Customize the used templates to make this package fit to your needs.
Publish the views with the command :
php artisan vendor:publish --tag=laravel-table:views
:warning: All the following methods are chainable with \Okipa\LaravelTable\Table
object except the ->column() and the ->result() methods (returning respectively \Okipa\LaravelTable\Column
and \Okipa\LaravelTable\Result
objects).
Set the model used during the table generation.
Notes:
model(string $tableModel): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->model(\App\News::class);
Set the table identifier, in order to automatically generate its id and to customize all the interaction fields in case of multiple tables used on a single view : the interactions with the table like sorting, searching an more will only have an impact on the identified table.
Notes:
identifier(string $identifier): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->identifier('Your identifier');
Set the request used for the table generation.
Notes:
request(Request $request): \Okipa\LaravelTable\Table
Use case example :
// example in a controller
public function index(Request $request) {
app(\Okipa\LaravelTable\\Okipa\LaravelTable\Table::class)->request($request);
// ...
}
Set the routes used during the table generation.
The routes declarations will be used for the following features :
index
(required) : used for the rows number definition, sort and search features.create
(optional) : the create button is displayed if this route is declared. The button used this route to redirect to the model creation page.edit
(optional) : the edit button is displayed on each row if this route is declared. The route is used to redirect to the model edition page.destroy
(optional) : the destroy button is displayed on each row if this route is declared. The route is used to trigger the model destroy action.show
(optional) : the show button is displayed on each row if this route is declared. The route is used to redirect to the model show page.
Note :
routes(array $routes): \Okipa\LaravelTable\Table
// example
[
'index' => [
// required
'name' => 'news.index',
// optional
'params' => [
// set route params
// or do not declare it
]
]
];
edit
, destroy
and show
routes, you do not have to define it in the ->routes()
call. You also should declare your routes carefully to avoid errors. See the examples bellow : // assuming your declared your route with implicit binding :
Route::get('parent/{$parent}/user/edit/{$user}/child/{$child}', 'UsersController@edit')->name('user.edit');
// you'll have to declare your params with keys as following :
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => ['parent' => $parent, 'child' => $child]],
// ...
])
// because the route will be generated with the table related model as first param (the params order differs from the declaration) :
route('user.edit, [$user, 'parent' => $parent, 'child' => $child]);
// now imagine your route is declared with the table related model as first param like this :
Route::get('/user/edit/{$user}/child/{$child}/{otherParam}', 'UsersController@edit')->name('user.edit');
// in this case only, you will be able to declare your routes without keys :
(new Table)->model(User::class)->routes([
// ...
'edit' => ['name'=> 'user.edit', 'params' => [$child, 'otherValue']],
// ...
])
// because the route params are given in the same order as the route declaration :
route('user.edit, [$user, $child, 'otherValue']);
Use case example :
(new \Okipa\LaravelTable\Table)->routes([
'index' => ['name' => 'news.index'],
'create' => ['name' => 'news.create', 'params' => ['param1' => 'value1']],
'edit' => ['name' => 'news.edit', 'params' => ['param2' => 'value2']],
'destroy' => ['name' => 'news.destroy'],
'show' => ['name' => 'news.show'],
]);
Override the config default number of rows displayed on the table.
The default number of displayed rows is defined in theconfig('laravel-table.value.rowsNumber')
config value.
Setfalse
to display all the models contained in database.
Note :
rowsNumber(?int $rows): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->rowsNumber(50);
// or
(new \Okipa\LaravelTable\Table)->rowsNumber(null);
Override the default rows number selection activation status.
Calling this method displays a rows number input that enable the user to choose how much rows to show.
The default rows number selection activation status is managed by theconfig('laravel-table.value.rowsNumberSelectionActivation')
value.
Note :`
rowsNumberSelectionActivation($activate = true): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->rowsNumberSelectionActivation(false);
Set the query closure that will be executed during the table generation.
For example, you can define your joined tables here.
The closure let you manipulate the following attribute :$query
.
Note :
query(Closure $queryClosure): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->query(function($query){
$query->select('users.*');
$query->addSelect('companies.name as company');
$query->join('users', 'users.id', '=', 'companies.owner_id');
});
Add an array of arguments to append to the paginator and to the following table actions :
- row number selection
- searching
- search canceling
- sorting.
Note :
appends(array $appendedValues): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->appends(request()->only('status'));
Override default table container classes.
The default container classes are defined in theconfig('laravel-table.classes.container')
config value.
Note :
containerClasses(array $containerClasses): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->containerClasses(['set', 'your', 'classes']);
Override default table classes.
The default table classes are defined in theconfig('laravel-table.classes.table')
config value.
Note :
tableClasses(array $tableClasses): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->tableClasses(['set', 'your', 'classes']);
Override default table tr classes.
The default tr classes are defined in theconfig('laravel-table.classes.tr')
config value.
Note :
trClasses(array $trClasses): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->trClasses(['set', 'your', 'classes']);
Override default table tr classes.
The default th classes are defined in theconfig('laravel-table.classes.th')
config value.
Note :
thClasses(array $thClasses): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->thClasses(['set', 'your', 'classes']);
Override default table td classes.
The default td classes are defined in theconfig('laravel-table.classes.td')
config value.
Note :
tdClasses(array $tdClasses): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->tdClasses(['set', 'your', 'classes']);
Set rows classes when the given conditions are respected.
The closure let you manipulate the following attribute :$model
. ` Note :
rowsConditionalClasses(Closure $rowClassesClosure, array $rowClasses): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->rowsConditionalClasses(function($model){
return $model->hasParticularAttribute;
}, ['set', 'your', 'classes']);
Define html attributes on the destroy buttons to handle dynamic javascript destroy confirmations.
The closure let you manipulate the following attribute :$model
.
Beware : the management of the destroy confirmation is on you, if you do not setup a javascript treatment to ask a confirmation, the destroy action will be directly executed.
Note :
destroyConfirmationHtmlAttributes(Closure $destroyConfirmationClosure): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->destroyHtmlAttributes(function($model){
return ['data-confirm' => __('Are you sure you want to delete the user :name ?', [
'name' => $model->name
])];
});
Javascript snippet example :
// example of javascript snippet to ask a confirmation before executing the action
// this example assume that a bootstrap modal component has been included in your view
// https://getbootstrap.com/docs/4.3/components/modal/#modal-components
const destroyButton = $('form.destroy button[type="submit"]');
destroyButton.click((e) => {
e.preventDefault();
const $this = $(e.target);
const message = $this.data("confirm");
const confirmationModal = $("#confirmationModal");
confirmationModal.find(".modal-body").text(message);
confirmationModal.modal("show");
});
Set the disable lines closure that will be executed during the table generation.
The optional second param let you override the classes that will be applied for the disabled lines.
By default, the « config('laravel-table.classes.disabled') » config value is applied.
For example, you can disable the current logged user to prevent him being edited or deleted from the table.
The closure let you manipulate the following attribute :$model
.
Note :
disableRows(Closure $rowDisableClosure, array $classes = []): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->disableRows(function($user){
return $user->id = auth()->id;
}, ['bg-danger', 'text-primary']);
Set a custom template path for the table component.
The default table template path is defined in theconfig('laravel-table.template.table')
config value.
Note :
tableTemplate(string $tableComponentPath): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->tableTemplate('tailwindCss.table');
Set a custom template path for the thead component.
The default thead template path is defined in theconfig('laravel-table.template.thead')
config value.
Note :
theadTemplate(string $theadComponentPath): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->theadTemplate('tailwindCss.thead');
Set a custom template path for the tbody component.
The default tbody template path is defined in theconfig('laravel-table.template.tbody')
config value.
Note :
tbodyTemplate(string $tbodyComponentPath): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->tbodyTemplate('tailwindCss.tbody');
Set a custom template path for the results component.
The default results template path is defined in theconfig('laravel-table.template.results')
config value.
Note :
resultsTemplate(string $resultsComponentPath): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->resultsComponentPath('tailwindCss.results');
Set a custom template path for the tfoot component.
The default tfoot template path is defined in theconfig('laravel-table.template.tfoot')
config value.
Note :
tfootTemplate(string $tfootComponentPath): \Okipa\LaravelTable\Table
Use case example :
(new \Okipa\LaravelTable\Table)->tfootTemplate('tailwindCss.tfoot');
Add a column that will be displayed in the table.
The column key is optional if the column is not declared as sortable or searchable.
Note :
column(string $databaseColumn = null): \Okipa\LaravelTable\Column
\Okipa\LaravelTable\Table
methods because it returns a \Okipa\LaravelTable\Column
object. See the use case examples to check how to use this method.Use case example :
(new \Okipa\LaravelTable\Table)->column('email');
Add a result row that will be displayed at the bottom of the table.
Note :
result(): Result
\Okipa\LaravelTable\Table
methods because it returns a \Okipa\LaravelTable\Result
object. See the use case examples to check how to use this method.Use case example :
(new \Okipa\LaravelTable\Table)->result();
:warning: All the column methods are chainable with \Okipa\LaravelTable\Column
object.
Set the custom classes that will be applied on this column only.
Note :
classes(array $classes): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column()->classes(['font-weight-bold']);
Set the column title or override the default (
__('validation.attributes.[column key])
) title generated from the column name.
Note :
title(string $title = null): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column()->title('E-mail');
Make the column sortable.
You also can choose to set the column sorted by default.
If no column is sorted by default, the first one will be automatically sorted.
Note :
sortable(bool $sortByDefault = false, $sortDirection = 'asc'): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column('email')->sortable();
// alternative
(new \Okipa\LaravelTable\Table)->column('email')->sortable(true, 'desc');
Make the column searchable.
The first param allows you to precise the searched database table (can references a database table alias).
The second param allows you to precise the searched database attributes (if not precised, the table database column is searched).
Note :
public function searchable(string $databaseSearchedTable = null, array $databaseSearchedColumns = []): \Okipa\LaravelTable\Column
Use case example :
// example 1
(new \Okipa\LaravelTable\Table)->column('email')->searchable();
// example 2
$table = (new \Okipa\LaravelTable\Table)->model(\App\User::class)->query(function($query) {
$query->select('users.*');
$query->addSelect('companies.name as company');
$query->join('companies', 'companies.owner_id', '=', 'users.id');
});
$table->column('company')->searchable('companies', ['name']);
// example 3
$table = (new \Okipa\LaravelTable\Table)->model(\App\User::class)->query(function($query) {
$query->select('users.*');
$query->addSelect(\DB::raw('CONCAT(companies.name, " ", companies.activity) as company'));
$query->join('companies as companiesAliasedTable', 'companies.owner_id', '=', 'users.id');
});
$table->column('company')->searchable('companiesAliasedTable', ['name', 'activity']);
Set the format for a datetime, date or time database column (optional).
(Carbon::parse($value)->format($format) method is used under the hood).
Note :
dateTimeFormat(string $dateTimeFormat): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column('created_at')->dateTimeFormat('d/m/Y H:i');
Display the column as a button with the given classes.
Note :
button(array $buttonClasses = []): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column('email')->button(['btn', 'btn-sm', 'btn-primary']);
Wrap the column value into a
<a></a>
component.
You can declare the link as a string or as a closure which will let you manipulate the following attributes :$model
,$column
.
If no url is declared, it will be set with the column value.
Note :
link($url = null): \Okipa\LaravelTable\Column
Use case example :
// example 1
(new \Okipa\LaravelTable\Table)->column('url')->link();
// example 2
(new \Okipa\LaravelTable\Table)->column()->link(route('news.index'));
// example 3
(new \Okipa\LaravelTable\Table)->column()->link(function($news) {
return route('news.show', $news);
});
Add an icon before the displayed value.
Set the second param as true if you want the icon to be displayed even if the column has no value.
Note :
icon(string $icon, bool $displaydisplayIconWhenNoValue = false): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column('email')->icon('<i class="fas fa-envelope"></i>', true);
Set the string value display limitation.
Shows "..." when the limit is reached.
Note :
stringLimit(int $stringLimit): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column('email')->stringLimit(30);
Display a custom value for the column.
The closure let you manipulate the following attributes :$model
,$column
.
Note :
value(Closure $valueClosure): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column()->value(function($user) {
return config('users.type.' . $user->type_id);
});
Display a custom HTML for the column.
The closure let you manipulate the following attributes :$model
,$column
.
Note :
html(Closure $htmlClosure): \Okipa\LaravelTable\Column
Use case example :
(new \Okipa\LaravelTable\Table)->column()->html(function($user) {
return '<div>' . $user->first_name . '</div>';
});
:warning: All the result methods are chainable with \Okipa\LaravelTable\Result
object.
Set the result row title.
Note :
title(string $title): \Okipa\LaravelTable\Result
Use case example :
(new \Okipa\LaravelTable\Table)->result()->title('Turnover total');
Display a HTML output for the result row.
The closure let you manipulate the following attributes :$displayedList
.
Note :
html(Closure $htmlClosure): \Okipa\LaravelTable\Result
Use case example :
(new \Okipa\LaravelTable\Table)->result()->html(function($displayedList) {
return $displayedList->sum('turnover');
});
Override the default results classes and apply the given classes only on this result row.
The default result classes are managed by theconfig('laravel-table.classes.results')
value.
Note :
classes(array $classes): \Okipa\LaravelTable\Result
Use case example :
(new \Okipa\LaravelTable\Table)->result()->classes(['bg-dark', 'text-white', 'font-weight-bold']);
request()
helper to get the number of lines to show and the searching, sorting or pagination data. However, if you need to pass a particular request to the table, you can do it with the ->request()
method.__('validation.attributes.[databaseColumn])
. You can set a custom title using the title()
method.->html()
method to build a custom display.
->button()
->link()
->icon()
->stringLimit()
->value()
In your controller, simply call the package like the following example to generate your table :
$table = (new \Okipa\LaravelTable\Table)->model(\App\News::class)->routes(['index' => ['name' => 'news.index']]);
$table->column('title')->sortable()->searchable();
Then, send your $table
object in your view and render your table like this :
{{ $table }}
That's it !
If you need your table for a more advanced usage, with a multilingual project for example, here is an example of what you can do in your controller :
$table = (new \Okipa\LaravelTable\Table)->model(\App\News::class)
->request($request)
->routes([
'index' => ['name' => 'news.index'],
'create' => ['name' => 'news.create'],
'edit' => ['name' => 'news.edit'],
'destroy' => ['name' => 'news.destroy'],
'show' => ['name' => 'news.show'],
])
->rowsNumber(50) // or set `false` to get all the items contained in database
->rowsNumberSelectionActivation(false)
->query(function ($query) use ($category_id) {
// some examples of what you can do
$query->select('news.*');
// add a constraint
$query->where('category_id', $category_id);
// get value stored in a json field
$query->addSelect('news.json_field->>json_attribute as json_attribute');
// get a formatted value form a pivot table
$query->selectRaw('count(comments.id) as comments_count');
$query->leftJoin('news_commment', 'news_commment.news_id', '=', 'news.id');
$query->leftJoin('comments', 'comments.id', '=', 'news_commment.comment_id');
$query->groupBy('comments.id');
// alias a value to make it available from the column model
$query->addSelect('users.name as author');
$query->join('users', 'users.id', '=', 'news.author_id');
})
->disableRows(function($model){
return $model->id === 1 || $model->id === 2;
}, ['disabled', 'bg-secondary'])
->rowsConditionalClasses(function($model){
return $model->id === 3;
}, ['highlighted', 'bg-success']);
$table->column('image')->html(function ($model, $column) {
return $model->{$column->databaseDefaultColumn}
? '<img src="' . $model->{$column->databaseDefaultColumn} . '" alt="' . $model->title . '">'
: null;
});
$table->column('title')->sortable()->searchable();
$table->column('content')->stringLimit(30);
$table->column('author')->sortable()->searchable('user', ['name']);
$table->column('category_id')
->title('Category custom name')
->icon('your-icon')
->button(['btn', 'btn-sm', 'btn-outline-primary'])
->value(function ($model, $column) {
return config('news.category.' . $model->{$column->databaseDefaultColumn});
});
$table->column()->link(function($model){
return route('news.show', $model);
})->button(['btn', 'btn-sm', 'btn-primary']);
$table->column('released_at')->sortable()->dateTimeFormat('d/m/Y H:i:s');
$table->result()->title('Total of comments')->html(function($displayedList){
return $displayedList->sum('comments_count');
});
composer test
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
The MIT License (MIT). Please see License File for more information.