Skip to content

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.

bash
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.

shell
     __     ____                          ______     _     __
    / /_,  / __ \____ _      _____  _____/ ____/____(_)___/ /
    /_ ,' / /_/ / __ \ | /| / / _ \/ ___/ / __/ ___/ / __  / 
    /'   / ____/ /_/ / |/ |/ /  __/ /  / /_/ / /  / / /_/ /  
        /_/    \____/|__/|__/\___/_/   \____/_/  /_/\__,_/     

 โ”Œ 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.

shell
 โ”Œ 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.

shell
 โ”Œ 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.

shell
 โ”Œ 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:

plain
โšก 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:

bash
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.

bash
php artisan powergrid:create --template=stubs/custom-component.stub

Custom Namespace โ€‹

See Custom Namespace.

Custom Model Location โ€‹

See Auto-Discover Models.

Created By Luan Freitas and @DanSysAnalyst