Skip to content

Commit 11e0ae6

Browse files
authored
Update README.md
1 parent 73e202d commit 11e0ae6

File tree

1 file changed

+134
-24
lines changed

1 file changed

+134
-24
lines changed

README.md

Lines changed: 134 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ Filament Tree is a plugin for Filament Admin that creates a model management pag
99

1010
This plugin creates model management page with heritage tree structure view for Filament Admin. It could be used to create menu, etc.
1111

12-
Demo site : https://filament-cms-website-demo.solutionforest.net/
12+
Demo site : https://filament-cms-website-demo.solutionforest.net/admin
1313

1414
Demo username : demo@solutionforest.net
1515

@@ -60,31 +60,31 @@ return [
6060

6161
## Usage
6262

63-
### Creating a Filament Resource Page
64-
65-
To create a resources page, run the following command:
66-
67-
```bash
68-
php artisan make:filament-resource ProductCategory
69-
```
70-
71-
Next, prepare the database and model.
72-
73-
### Table Structure and Model
63+
### Prepare the database and model
7464

7565
To use Filament Tree, follow these table structure conventions:
7666

7767
> **Tip: The `parent_id` field must always default to -1!!!**
7868
7969
```
8070
Schema::create('product_categories', function (Blueprint $table) {
81-
$table->id();
82-
$table->integer('parent_id')->default(-1);
83-
$table->integer('order')->default(0)->index();
84-
$table->string('title');
85-
$table->timestamps();
86-
});
71+
$table->id();
72+
$table->integer('parent_id')->default(-1);
73+
$table->integer('order')->default(0)->index();
74+
$table->string('title');
75+
$table->timestamps();
76+
});
77+
```
78+
This plugin provides a convenient method called `treeColumns()` that you can use to add the required columns for the tree structure to your table more easily. Here's an example:
8779
```
80+
Schema::create('product_categories', function (Blueprint $table) {
81+
$table->id();
82+
$table->treeColumns();
83+
$table->timestamps();
84+
});
85+
```
86+
This will automatically add the required columns for the tree structure to your table.
87+
8888

8989
The above table structure contains three required fields: `parent_id`, `order`, `title`, and other fields do not have any requirements.
9090

@@ -157,15 +157,28 @@ class ProductCategory extends Model
157157

158158
```
159159

160-
### Create Tree Widget
160+
### Widget
161+
Filament provides a powerful feature that allows you to display widgets inside pages, below the header and above the footer. This can be useful for adding additional functionality to your resource pages.
162+
163+
To create a Tree Widget and apply it to a resource page, you can follow these steps:
164+
165+
#### 1. Creating a Filament Resource Page
166+
To create a resources page, run the following command:
167+
168+
```
169+
php artisan make:filament-resource ProductCategory
170+
```
171+
172+
#### 2. Create Tree Widget
161173

162174
Prepare the filament-tree Widget and show it in Resource page.
163175

164-
```bash
176+
```php
165177
php artisan make:filament-tree-widget ProductCategoryWidget
166178
```
179+
167180
Now you can see the Widget in Filament Folder
168-
```
181+
``` php
169182
<?php
170183

171184
namespace App\Filament\Widgets;
@@ -178,6 +191,7 @@ class ProductCategoryWidget extends BaseWidget
178191
{
179192
protected static string $model = ModelsProductCategory::class;
180193

194+
// you can customize the maximum depth of your tree
181195
protected static int $maxDepth = 2;
182196

183197
protected ?string $treeTitle = 'ProductCategory';
@@ -187,9 +201,9 @@ class ProductCategoryWidget extends BaseWidget
187201
```
188202

189203

190-
### Resource Page
204+
#### 3. Displaying a widget on a resource page
191205

192-
Once you have created the widget, modify the resource page to show the tree view:
206+
Once you have created the widget, modify the `getHeaderWidgets()` or `getFooterWidgets()` methods of the resource page to show the tree view:
193207

194208
```php
195209
<?php
@@ -220,8 +234,104 @@ class ListProductCategories extends ListRecords
220234
}
221235
}
222236
```
237+
238+
### Resources
239+
Filament allows you to create a custom pages for resources, you also can create a tree page that display hierarchical data.
240+
#### Create a Page
241+
To create a tree page for resource, you can use:
242+
```
243+
php artisan make:filament-tree-page ProductCategoryTree --resource=ProductCategory
244+
```
245+
246+
#### Register a Page to the resource
247+
You must register the tree page to a route in the static `getPages()` methods of your resource. For example:
248+
249+
``` php
250+
public static function getPages(): array
251+
{
252+
return [
253+
// ...
254+
'tree-list' => Pages\ProductCategoryTree::route('/tree-list'),
255+
];
256+
}
257+
```
258+
#### Actions
259+
Define the available "actions" for the tree page using the `getActions()` and `getTreeActions()` methods of your page class.
260+
261+
The `getActions()` method defines actions that are displayed next to the page's heading:
262+
```php
263+
use Filament\Pages\Actions\CreateAction;
264+
265+
protected function getActions(): array
266+
{
267+
return [
268+
CreateAction::make(),
269+
// SAMPLE CODE, CAN DELETE
270+
//\Filament\Pages\Actions\Action::make('sampleAction'),
271+
];
272+
}
273+
```
274+
275+
The `getTreeActions()` method defines the actions that are displayed for each record in the tree. For example:
276+
```php
277+
use Filament\Pages\Actions\Action;
278+
279+
protected function getTreeActions(): array
280+
{
281+
return [
282+
Actions\ViewAction::make(),
283+
Actions\EditAction::make(),
284+
Actions\DeleteAction::make(),
285+
];
286+
}
287+
288+
```
289+
290+
Alternatively, you can use the `hasDeleteAction()`, `hasEditAction()`, and `hasViewAction()` methods to customize each action individually.
291+
292+
``` php
293+
protected function hasDeleteAction(): bool
294+
{
295+
return false;
296+
}
297+
298+
protected function hasEditAction(): bool
299+
{
300+
return true;
301+
}
302+
303+
protected function hasViewAction(): bool
304+
{
305+
return false;
306+
}
307+
```
308+
#### Record ICON
309+
To customize the prefix icon for each record in a tree page, you can use the `getTreeRecordIcon()` method in your tree page class. This method should return a string that represents the name of the icon you want to use for the record. For example:
310+
311+
```php
312+
public function getTreeRecordIcon(?\Illuminate\Database\Eloquent\Model $record = null): ?string
313+
{
314+
// default null
315+
return 'heroicon-o-cake';
316+
}
317+
```
318+
![tree-icon](https://github.com/solutionforest/filament-tree/assets/68525320/6a1ef719-9029-4e91-a20a-515a514c4326)
319+
320+
321+
### Pages
322+
This plugin enables you to create tree pages in the admin panel. To create a tree page for a model, use the `make:filament-tree-page` command. For example, to create a tree page for the ProductCategory model, you can run:
323+
#### Create a Page
324+
> **Tip: Note that you should make sure the model contains the required columns or already uses the `ModelTree` trait**
325+
```php
326+
php artisan make:filament-tree-page ProductCategory --model=ProductCategory
327+
```
328+
329+
#### Actions, Widgets and Icon for each record
330+
Once you've created the tree page, you can customize the available actions, widgets, and icon for each record. You can use the same methods as for resource pages. See the [Resource Page](#resources) for more information on how to customize actions, widgets, and icons.
331+
332+
223333

224-
### Publishing Configuration
334+
### Publishing Views
225335

226336
To publish the views, use:
227337

0 commit comments

Comments
 (0)