在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):Okipa/laravel-table开源软件地址(OpenSource Url):https://github.com/Okipa/laravel-table开源编程语言(OpenSource Language):PHP 92.0%开源软件介绍(OpenSource Introduction):Easily render tables from Eloquent models in your views. This package is shipped with a pre-configuration for Found this package helpful? Please consider supporting my work! Compatibility
Upgrade guideUsageCreate your table class with the following command: php artisan make:table UsersTable --model=App/Models/User Set your table configuration in the generated file, which can be found in the namespace App\Tables;
use Okipa\LaravelTable\Abstracts\AbstractTable;
use Okipa\LaravelTable\Table;
use App\Models\User;
class UsersTable extends AbstractTable
{
protected function table(): Table
{
return (new Table())->model(User::class)
->routes([
'index' => ['name' => 'users.index'],
'create' => ['name' => 'user.create'],
'edit' => ['name' => 'user.edit'],
'destroy' => ['name' => 'user.destroy'],
])
->destroyConfirmationHtmlAttributes(fn(User $user) => [
'data-confirm' => __('Are you sure you want to delete the user :name ?', [
'name' => $user->name
])
]);
}
protected function columns(Table $table): void
{
$table->column('id')->sortable(true);
$table->column('name')->sortable()->searchable();
$table->column('email')->sortable()->searchable();
$table->column('created_at')->dateTimeFormat('d/m/Y H:i', 'Europe/Paris')->sortable();
$table->column('updated_at')->dateTimeFormat('d/m/Y H:i', 'Europe/Paris')->sortable();
}
} Send the table to your view: use \Illuminate\View\View;
use \App\Tables\UsersTable;
class UsersController
{
public function index(): View
{
$table = (new UsersTable())->setup();
return view('templates.users.index', compact('table'));
}
} Finally, display it in the view: {{ $table }} Table of contents
Installation
composer require okipa/laravel-table ConfigurationOptionally publish the package configuration: php artisan vendor:publish --tag=laravel-table:config TemplatesOptionally publish the package templates: php artisan vendor:publish --tag=laravel-table:views TranslationsAll words and sentences used in this package are translatable. See how to translate them on the Laravel official documentation: https://laravel.com/docs/localization#using-translation-strings-as-keys. Here is the list of the words and sentences available for translation:
Advanced configuration examplenamespace App\Tables;
use App\News;
use Illuminate\Support\Str;
use Illuminate\Http\Request;
use Illuminate\Database\Eloquent\Builder;
use Okipa\LaravelTable\Table;
use Okipa\LaravelTable\Abstracts\AbstractTable;
class NewsTable extends AbstractTable
{
protected Request $request;
protected int $categoryId;
public function __construct(Request $request, int $categoryId)
{
$this->request = $request;
$this->categoryId = $categoryId;
}
protected function table(): Table
{
return (new Table())->model(News::class)
->identifier('news-table')
->request($this->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 `null` to display all the items contained in database
->activateRowsNumberDefinition(false)
->query(function (Builder $query) {
// Some examples of what you can do
$query->select('news.*');
// Add a constraint
$query->where('category_id', $this->categoryId);
// Get value stored in a json field
$query->addSelect('news.json_field->>json_attribute as json_attribute');
// Get a formatted value from 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(fn(News $news) => in_array($news->id, [1, 2]), ['disabled', 'bg-secondary', 'text-white'])
->rowsConditionalClasses(fn(News $news) => $news->id === 3, ['highlighted', 'bg-success'])
->rowsConditionalClasses(
fn(News $news) => $news->category,
fn(News $news) => 'category-' . Str::snake($news->category)
)
// Append all request params to the paginator
->appendData($this->request->all());
}
protected function columns(Table $table): void
{
$table->column('id')->sortable(true);
$table->column()->title(__('Illustration'))->html(fn(News $news) => $news->image_src
? '<img src="' . $news->image_src . '" alt="' . $news->title . '">'
: null);
$table->column('title')->sortable()->searchable();
$table->column('content')->stringLimit(30);
$table->column('author')->sortable(true)->searchable('user', ['name']);
$table->column('category_id')
->title(__('Category'))
->prependHtml('<i class="fas fa-hand-point-right"></i>')
->appendsHtml('<i class="fas fa-hand-point-left"></i>')
->button(['btn', 'btn-sm', 'btn-outline-primary'])
->value(fn(News $news) => config('news.category.' . $news->category_id))
$table->column()
->title(__('Display'))
->link(fn(News $news) => route('news.show', $news))
->button(['btn', 'btn-sm', 'btn-primary']);
$table->column('created_at')->dateTimeFormat('d/m/Y H:i', 'Europe/Paris')->sortable();
$table->column('updated_at')->dateTimeFormat('d/m/Y H:i', 'Europe/Paris')->sortable();
$table->column('published_at')->dateTimeFormat('d/m/Y H:i', 'Europe/Paris')->sortable(true, 'desc');
}
protected function resultLines(Table $table): void
{
$table->result()
->title('Total of comments')
->html(fn(Collection $paginatedRows) => $paginatedRows->sum('comments_count'));
}
} Tips
Table API
model
Notes:
Use case example: (new Table())->model(User::class); identifier
Notes:
Use case example: (new Table())->identifier('users-table'); request
Notes:
Use case example: Pass the request to your table: class UsersController
{
public function index(\Illuminate\Http\Request $request)
{
$table = new UsersTable($request);
// ...
}
} Then, use the custom request in your table: namespace App\Tables;
use App\Models\Users;
use Illuminate\Http\Request;
use Okipa\LaravelTable\Abstracts\AbstractTable;
use Okipa\LaravelTable\Table;
class UsersTable extends AbstractTable
{
protected Request $request;
public function __construct(Request $request)
{
$this->request = $request;
}
protected function table(): Table
{
return (new Table())->model(User::class)->request($this->request);
}
// ...
} routes
Note:
// Example
[
'index' => [
// Required
'name' => 'users.index',
// Optional
'params' => [
// Set route params (or not)
]
]
// You will have to respect the same structure for any declared route.
];
// Assuming your declared your route with implicit binding:
Route::get('parent/{$parent}/user/edit/{$user}/child/{$child}', 'UsersController@edit')->name('user.edit');
// You will 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, 'otherParam']],
// ...
]);
// Because the route params are given in the same order as the route declaration:
route('user.edit', [$user, $child, 'otherParam']); Use case example: (new 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'],
]); rowsNumber
Note:
Use case example: (new Table())->rowsNumber(50);
// Or
(new Table())->rowsNumber(null); activateRowsNumberDefinition
Note:`
Use case example: (new Table())->activateRowsNumberDefinition(false); |