Create a PowerGrid Table โ
This section covers the the process of creating a PowerGrid Component.
Here you will find:
Introduction โ
Now that you've finished the PowerGrid Essential Configuration, it's time create your very first Table Component!
Create a Table โ
To create a PowerGrid Table, run the following command in your Laravel project.
php artisan powergrid:create
The assistant will guide you through the process and help you generate your Table Component.
1. Name the Component โ
First, you need to name your new Table Component.
In this example, let's create a component called "DishTable" to list all dishes in a food menu.
__ ____ ______ _ __
/ /_, / __ \____ _ _____ _____/ ____/____(_)___/ /
/_ ,' / /_/ / __ \ | /| / / _ \/ ___/ / __/ ___/ / __ /
/' / ____/ /_/ / |/ |/ / __/ / / /_/ / / / / /_/ /
/_/ \____/|__/|__/\___/_/ \____/_/ /_/\__,_/
โ What is the name of your Table Component? โโโโโโโโโโโโโโโโโโโโ
โ DishTable โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
To create your Component in a subdirectory, just enter the directory path followed by the component name. For example: Tables/Dishes/DishTable
.
If your application is organized in a different architecture (E.g, Domain-Driven Design), proceed to read more on how to configure a Custom Namespace for PowerGrid Components.
2. Select the Data Source โ
Now, configure the data source from which your Table will pull data from.
2.1. Select the data source โ
Select the data source type.
In our example, we will use Laravel's Eloquent Builder.
โ What type of data source will you use? โโโโโโโโโโโโโโโโโโโโโโโ
โ โบ โ Eloquent Builder โ
โ โ Query Builder โ
โ โ Collection โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
2.2. Model โ
Here, you need to select a Model to be linked to the component.
Following our example, we will use the Dish
Model.
โ Select a Model or enter its Fully qualified name. โโโโโโโโโโโโ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โโบ App\Models\Dish โ
โ App\Models\FooBar โ
โ App\Models\FoorBarBaz โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
By default, PowerGrid suggests Eloquent Models placed in the default path app/Models
.
You can customize the Auto-Discover Models if your application is organized in a different architecture (E.g, Domain-Driven Design).
2.3. Fillable โ
If desired, PowerGrid has the capability to automatically generate certain Table Columns based on the fields specified in your Model's fillable
property.
Sure, let's proceed with "yes" for this example.
โ Create columns based on Model's fillable property? โโโโโโโโโโโ
โ โ Yes / โ No โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
Note: This feature is available only for MySQL, PostgreSQL, and SQLite databases.
3. Use Your Table โ
โก Your PowerGrid Table is ready!
At this step, you should see a message that looks like this:
โก DishTable was successfully created at [app/Livewire/DishTable.php].
๐ก include the DishTable component using the tag: <livewire:dish-table/>
In the feedback message, you will find:
- The file path where your Component was created.
- The HTML tag to include it in your Blade View.
๐ That's it!
Now we can proceed to the Show a PowerGrid Table section.
Customize the Component Creation โ
Component Stubs โ
You may customize the default PowerGrid Component, adapting it to your needs.
To publish the stub, run the following command:
php artisan powergrid:publish --type=stub
If you need to create multiple stubs, be sure to rename the file after publishing each stub.
You may use the flag --template
passing the full location of your stub when creating a new component.
php artisan powergrid:create --template=stubs/custom-component.stub
Custom Namespace โ
See Custom Namespace.
Custom Model Location โ
See Auto-Discover Models.