PHP code example of evgeniy-silantev / laravel-table
1. Go to this page and download the library: Download evgeniy-silantev/laravel-table 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/ */
evgeniy-silantev / laravel-table example snippets
$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();
(new \Okipa\LaravelTable\Table)->model(\App\News::class);
(new \Okipa\LaravelTable\Table)->identifier('Your identifier');
// example in a controller
public function index(Request $request) {
app(\Okipa\LaravelTable\\Okipa\LaravelTable\Table::class)->request($request);
// ...
}
// example
[
'index' => [
// // optional
'params' => [
// set route params
// or do not declare it
]
]
];
// 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']);
(new \Okipa\LaravelTable\Table)->rowsNumber(50);
// or
(new \Okipa\LaravelTable\Table)->rowsNumber(null);
(new \Okipa\LaravelTable\Table)->rowsNumberSelectionActivation(false);
(new \Okipa\LaravelTable\Table)->query(function($query){
$query->select('users.*');
$query->addSelect('companies.name as company');
$query->join('users', 'users.id', '=', 'companies.owner_id');
});
(new \Okipa\LaravelTable\Table)->appends(request()->only('status'));
(new \Okipa\LaravelTable\Table)->containerClasses(['set', 'your', 'classes']);
(new \Okipa\LaravelTable\Table)->tableClasses(['set', 'your', 'classes']);
(new \Okipa\LaravelTable\Table)->trClasses(['set', 'your', 'classes']);
(new \Okipa\LaravelTable\Table)->thClasses(['set', 'your', 'classes']);
(new \Okipa\LaravelTable\Table)->tdClasses(['set', 'your', 'classes']);
(new \Okipa\LaravelTable\Table)->rowsConditionalClasses(function($model){
return $model->hasParticularAttribute;
}, ['set', 'your', 'classes']);
(new \Okipa\LaravelTable\Table)->destroyHtmlAttributes(function($model){
return ['data-confirm' => __('Are you sure you want to delete the user :name ?', [
'name' => $model->name
])];
});
(new \Okipa\LaravelTable\Table)->disableRows(function($user){
return $user->id = auth()->id;
}, ['bg-danger', 'text-primary']);
(new \Okipa\LaravelTable\Table)->tableTemplate('tailwindCss.table');
(new \Okipa\LaravelTable\Table)->theadTemplate('tailwindCss.thead');
(new \Okipa\LaravelTable\Table)->tbodyTemplate('tailwindCss.tbody');
(new \Okipa\LaravelTable\Table)->resultsComponentPath('tailwindCss.results');
(new \Okipa\LaravelTable\Table)->tfootTemplate('tailwindCss.tfoot');
(new \Okipa\LaravelTable\Table)->column('email');
(new \Okipa\LaravelTable\Table)->result();
(new \Okipa\LaravelTable\Table)->column()->classes(['font-weight-bold']);
(new \Okipa\LaravelTable\Table)->column()->title('E-mail');
(new \Okipa\LaravelTable\Table)->column('email')->sortable();
// alternative
(new \Okipa\LaravelTable\Table)->column('email')->sortable(true, 'desc');
// 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']);
(new \Okipa\LaravelTable\Table)->column('created_at')->dateTimeFormat('d/m/Y H:i');
(new \Okipa\LaravelTable\Table)->column('email')->button(['btn', 'btn-sm', 'btn-primary']);
// 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);
});
(new \Okipa\LaravelTable\Table)->column('email')->icon('<i class="fas fa-envelope"></i>', true);
(new \Okipa\LaravelTable\Table)->column('email')->stringLimit(30);
(new \Okipa\LaravelTable\Table)->column()->value(function($user) {
return config('users.type.' . $user->type_id);
});
(new \Okipa\LaravelTable\Table)->column()->html(function($user) {
return '<div>' . $user->first_name . '</div>';
});
(new \Okipa\LaravelTable\Table)->result()->title('Turnover total');
(new \Okipa\LaravelTable\Table)->result()->html(function($displayedList) {
return $displayedList->sum('turnover');
});
(new \Okipa\LaravelTable\Table)->result()->classes(['bg-dark', 'text-white', 'font-weight-bold']);
$table = (new \Okipa\LaravelTable\Table)->model(\App\News::class)->routes(['index' => ['name' => 'news.index']]);
$table->column('title')->sortable()->searchable();
$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');
});