从 Eloquent 模型生成表格。
I've been quite busy professionally lately and I'm currently looking for one or more co-maintainers to help me to maintain and develop this package.
Current roadmap:
Please contact me at arthur[dot]lorent[at]gmail.com to apply.
Save time and easily render tables in your views from Eloquent models.
Tables can be generated under the following UI frameworks:
Found this package helpful? Please consider supporting my work!
| Laravel version | Livewire version | PHP version | Package version |
|---|---|---|---|
| ^9.0 | ^10.0 | ^2.0 | 8.1.* | 8.2.* | ^5.3 |
| ^8.0 | ^9.0 | ^2.0 | ^8.1 | ^5.0 |
| ^7.0 | ^8.0 | X | ^7.4 | ^8.0 | ^4.0 |
| ^7.0 | ^8.0 | X | ^7.4 | ^8.0 | ^3.0 |
| ^6.0 | ^7.0 | X | ^7.4 | ^8.0 | ^2.0 |
| ^5.8 | ^6.0 | ^7.0 | X | ^7.2 | ^7.3 | ^7.4 | ^1.3 |
| ^5.5 | ^5.6 | ^5.7 | ^5.8 | ^6.0 | X | ^5.8 | ^7.1 | ^1.0 |
Create your table with the following command:
php artisan make:table UsersTable --model=App/Models/UserConfigure your table in the UsersTable generated class, which can be found in the app\Tables directory:
…And display it in a view:
composer require okipa/laravel-tableThis package uses Livewire under the hood and its installation is required.
You'll have to follow the installation instructions if Livewire is not already installed on your project.
Optionally publish the package configuration:
php artisan vendor:publish --tag=laravel-table:configOptionally publish the package templates:
php artisan vendor:publish --tag=laravel-table:viewsAll 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:
Status
Loading in progress...No results were found.You can rearrange the order of the items in this list using a drag and drop action.Reset filtersYesNoSearch by:Reset researchNumber of rows per pageSort ascendingSort descendingActionsBulk ActionsCreateAddShowEditDestroyActivateDeactivateVerify EmailUnverify EmailToggle OnToggle OffAre you sure you want to execute the action :action on the line #:primary?Are you sure you want to execute the action :action on the field :attribute from the line #:primary?Are you sure you want to execute the action :action on the :count selected lines?The line #:primary does not allow the action :action and will not be affected.:count selected lines do not allow the action :action and will not be affected.The action :action has been executed on the line #:primary.The action :action has been executed on the field :attribute from the line #:primary.The action :action has been executed on the :count selected lines.The line #:primary does not allow the action :action and was not affected.:count selected lines do not allow the action :action and were not affected.The list has been reordered.Showing results :start to :stop on :totalGenerate a table configuration by executing this command : php artisan make:table UsersTable.
If you want to generate a configuration with a predefined model, just add this option at the end: --model=App/Models/User.
You'll find all your generated table configurations in the app/Tables directory.
Just call this Livewire component in your view with your configuration class name passed in the config parameter.
In case you have specific attributes to transmit to your table configuration, you should pass them to the configParams parameter.
This could be useful when you have to transmit external information to your table.
1]"/>You should then declare the passed attributes as public attributes your table configuration.
namespace App\Tables;
use App\Models\User;
use Okipa\LaravelTable\Table;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
class UsersTable extends AbstractTableConfiguration
{
// You will now be able to use the provided `$this->categoryId` category ID in your table configuration.
public int $categoryId;
// ...
}To generate a table from an Eloquent model, you'll just have to call the model method on your table.
namespace App\Tables;
use App\Models\User;
use Okipa\LaravelTable\Table;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
class UsersTable extends AbstractTableConfiguration
{
protected function table(): Table
{
return Table::make()->model(User::class);
}
}You may want to override native HTML select components behaviour on your tables.
You will be able to add a data attribute (which is known as the best practice to add extra features to a HTML component) to all the HTML select components displayed on your tables by defining an array of HTML attribute as value for the laravel-table.html_select_components_attributes config key.
// `data-selector` HTML attribute will be appended to all tables HTML select components.
'html_select_components_attributes' => ['data-selector' => true],To add specific query instructions on tables, use the available query method.
You'll be able to set specific Eloquent instructions by passing a closure parameter to the query method on your table.
This closure will allow you to manipulate a \Illuminate\Database\Eloquent\Builder $query argument.
namespace App\Tables;
use App\Models\User;
use Okipa\LaravelTable\Table;
use Illuminate\Database\Eloquent\Builder;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
class UsersTable extends AbstractTableConfiguration
{
protected function table(): Table
{
return Table::make()
->model(User::class)
->query(fn(Builder $query) => $query->where('category_id', 1));
}
}You have two ways to allow or disallow users to choose the number of rows that will be displayed per page:
laravel-table.enable_number_of_rows_per_page_choice config boolean valueenableNumberOfRowsPerPageChoice() method on your tablenamespace App\Tables;
use App\Models\User;
use Okipa\LaravelTable\Table;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
class UsersTable extends AbstractTableConfiguration
{
protected function table(): Table
{
return Table::make()
->model(User::class)
->enableNumberOfRowsPerPageChoice(false);
}
}Following the same logic, you'll be able to define the number of rows per page options that will be available for selection:
laravel-table.number_of_rows_per_page_default_options config array valuenumberOfRowsPerPageOptions() method on your tableThe first available option will be automatically selected and applied on table initialization.
namespace App\Tables;
use App\Models\User;
use Okipa\LaravelTable\Table;
use Okipa\LaravelTable\Abstracts\AbstractTableConfiguration;
class UsersTable extends AbstractTableConfiguration
{
protected function table(): Table
{
return Table::make()
->model(User::class)
// Table will display 5 rows on initialization and will allow displaying 10, 15, 20 or 25 rows.
->numberOfRowsPerPageOptions([5, 10, 15, 20, 25]);
}
}Pagination will automatically be handled, according to the number of rows to display and the total number of rows, as well as a navigation status.
Both of them will be displayed in the table footer.
Define conditional row class on tables by passing a closure argument to the rowClass method.
This closure will allow you to manipulate a Illuminate\Database\Eloquent $model argument and has to return an array of classes where the array key contains the class or classes you wish to add, while the value is a boolean expression.
namespace App\Tables;
use App\Models\User;
use Okipa\暂无开放 Issues,或尚未同步最近议题。