Skip to content

Commit afd7144

Browse files
committed
Update HelpArticle model
1 parent 4bf58cb commit afd7144

File tree

6 files changed

+120
-21
lines changed

6 files changed

+120
-21
lines changed

README.md

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ This is the contents of the published config file:
3131

3232
```php
3333
return [
34+
/*
35+
| Help Article Model
36+
|
37+
| If you extend the HelpArticle model, specify your extended model here.
38+
| This ensures Filament resources use your extended model.
39+
*/
40+
'model' => env('FILAMENT_HELP_MODEL', \Tapp\FilamentHelp\Models\HelpArticle::class),
41+
3442
'tenancy' => [
3543
// Enable or disable tenancy features globally
3644
'enabled' => env('FILAMENT_HELP_TENANCY_ENABLED', false),
@@ -225,21 +233,76 @@ Or use environment variables in your `.env` file:
225233

226234
```env
227235
FILAMENT_HELP_TENANCY_ENABLED=true
236+
FILAMENT_HELP_TENANCY_MODEL=App\Models\Team
228237
FILAMENT_HELP_TENANCY_COLUMN=team_id
238+
FILAMENT_HELP_TENANCY_RELATIONSHIP=team
229239
FILAMENT_HELP_TENANCY_SCOPE_ADMIN=true
230240
FILAMENT_HELP_TENANCY_SCOPE_FRONTEND=true
231241
FILAMENT_HELP_TENANCY_SCOPE_GUEST=false
232242
```
233243

234-
2. **Run migrations**:
244+
2. **Add the tenant relationship to your HelpArticle model**:
245+
246+
Since the package needs to support various tenant models (Team, Organization, etc.), you need to define the relationship in your application.
247+
248+
Extend the `HelpArticle` model in your application:
249+
250+
```php
251+
// app/Models/HelpArticle.php
252+
namespace App\Models;
253+
254+
use Illuminate\Database\Eloquent\Relations\BelongsTo;
255+
use Tapp\FilamentHelp\Models\HelpArticle as BaseHelpArticle;
256+
257+
class HelpArticle extends BaseHelpArticle
258+
{
259+
/**
260+
* Get the team that owns the help article.
261+
*/
262+
public function team(): BelongsTo
263+
{
264+
return $this->belongsTo(\App\Models\Team::class);
265+
}
266+
}
267+
```
268+
269+
Or if you use a different tenant model:
270+
271+
```php
272+
public function organization(): BelongsTo
273+
{
274+
return $this->belongsTo(\App\Models\Organization::class);
275+
}
276+
```
277+
278+
**Important**: Make sure the relationship name matches the `relationship` config value you set (e.g., `'team'` or `'organization'`).
279+
280+
Then, update your config to use your extended model:
281+
282+
```php
283+
// config/filament-help.php
284+
return [
285+
'model' => \App\Models\HelpArticle::class,
286+
287+
'tenancy' => [
288+
'enabled' => true,
289+
'model' => \App\Models\Team::class,
290+
'column' => 'team_id',
291+
'relationship' => 'team',
292+
// ...
293+
],
294+
];
295+
```
296+
297+
3. **Run migrations**:
235298

236299
When tenancy is enabled, the migration will automatically add the tenant column to the `help_articles` table:
237300

238301
```bash
239302
php artisan migrate
240303
```
241304

242-
3. **Configure your Filament panel with tenancy**:
305+
4. **Configure your Filament panel with tenancy**:
243306

244307
```php
245308
// In your AdminPanelProvider.php (or wherever you configure your Filament panel)

config/filament-help.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,21 @@
22

33
return [
44

5+
/*
6+
|--------------------------------------------------------------------------
7+
| Help Article Model
8+
|--------------------------------------------------------------------------
9+
|
10+
| If you extend the HelpArticle model in your application to add custom
11+
| relationships (e.g., tenant relationships), specify your extended model here.
12+
| This ensures Filament resources use your extended model instead of the base model.
13+
|
14+
| Example: \App\Models\HelpArticle::class
15+
|
16+
*/
17+
18+
'model' => \Tapp\FilamentHelp\Models\HelpArticle::class,
19+
520
/*
621
|--------------------------------------------------------------------------
722
| Tenancy Configuration

src/Models/HelpArticle.php

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Tapp\FilamentHelp\Models;
44

5-
use Filament\Models\Contracts\HasTenants;
65
use Illuminate\Database\Eloquent\Factories\HasFactory;
76
use Illuminate\Database\Eloquent\Model;
8-
use Illuminate\Database\Eloquent\Relations\BelongsTo;
97

108
class HelpArticle extends Model
119
{
@@ -46,27 +44,38 @@ public function scopeVisible($query)
4644
return $query->where('is_hidden', false);
4745
}
4846

49-
/**
50-
* Get the team that owns this help article.
51-
*/
52-
public function team(): BelongsTo
53-
{
54-
$tenantModel = config('filament-help.tenancy.model') ?? \App\Models\Team::class;
55-
$tenantColumn = config('filament-help.tenancy.column') ?? 'team_id';
56-
57-
return $this->belongsTo($tenantModel, $tenantColumn);
58-
}
59-
6047
/**
6148
* Scope query to only include articles for a specific tenant/team.
6249
*/
6350
public function scopeForTenant($query, $tenant)
6451
{
52+
if (! config('filament-help.tenancy.enabled', false)) {
53+
return $query;
54+
}
55+
6556
$tenantColumn = config('filament-help.tenancy.column') ?? 'team_id';
6657

6758
return $query->where($tenantColumn, $tenant->id);
6859
}
6960

61+
/**
62+
* Define your tenant relationship here.
63+
*
64+
* Example for Team:
65+
*
66+
* public function team(): BelongsTo
67+
* {
68+
* return $this->belongsTo(\App\Models\Team::class);
69+
* }
70+
*
71+
* Or for Organization:
72+
*
73+
* public function organization(): BelongsTo
74+
* {
75+
* return $this->belongsTo(\App\Models\Organization::class);
76+
* }
77+
*/
78+
7079
protected static function boot()
7180
{
7281
parent::boot();

src/Resources/Frontend/HelpArticleResource.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
use Filament\Support\Enums\Alignment;
88
use Filament\Tables\Columns\Layout\Stack;
99
use Filament\Tables\Table;
10-
use Tapp\FilamentHelp\Models\HelpArticle;
1110
use Tapp\FilamentHelp\Resources\Frontend\Pages\ListHelpArticles;
1211
use Tapp\FilamentHelp\Resources\Frontend\Pages\ViewHelpArticle;
1312
use Tapp\FilamentHelp\Tables\Components\HelpArticleCardColumn;
1413

1514
class HelpArticleResource extends Resource
1615
{
17-
protected static ?string $model = HelpArticle::class;
16+
protected static ?string $model = null;
17+
18+
public static function getModel(): string
19+
{
20+
return static::$model ?? config('filament-help.model', \Tapp\FilamentHelp\Models\HelpArticle::class);
21+
}
1822

1923
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-question-mark-circle';
2024

src/Resources/Guest/HelpArticleResource.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,18 @@
77
use Filament\Support\Enums\Alignment;
88
use Filament\Tables\Columns\Layout\Stack;
99
use Filament\Tables\Table;
10-
use Tapp\FilamentHelp\Models\HelpArticle;
1110
use Tapp\FilamentHelp\Resources\Guest\Pages\ListHelpArticles;
1211
use Tapp\FilamentHelp\Resources\Guest\Pages\ViewHelpArticle;
1312
use Tapp\FilamentHelp\Tables\Components\HelpArticleCardColumn;
1413

1514
class HelpArticleResource extends Resource
1615
{
17-
protected static ?string $model = HelpArticle::class;
16+
protected static ?string $model = null;
17+
18+
public static function getModel(): string
19+
{
20+
return static::$model ?? config('filament-help.model', \Tapp\FilamentHelp\Models\HelpArticle::class);
21+
}
1822

1923
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-question-mark-circle';
2024

src/Resources/HelpArticleResource.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,16 @@
1212
use Filament\Tables\Actions\BulkActionGroup;
1313
use Filament\Tables\Actions\DeleteBulkAction;
1414
use Illuminate\Support\Str;
15-
use Tapp\FilamentHelp\Models\HelpArticle;
1615
use Tapp\FilamentHelp\Resources\HelpArticleResource\Pages;
1716

1817
class HelpArticleResource extends Resource
1918
{
20-
protected static ?string $model = HelpArticle::class;
19+
protected static ?string $model = null;
20+
21+
public static function getModel(): string
22+
{
23+
return static::$model ?? config('filament-help.model', \Tapp\FilamentHelp\Models\HelpArticle::class);
24+
}
2125

2226
protected static string|\BackedEnum|null $navigationIcon = 'heroicon-o-question-mark-circle';
2327

0 commit comments

Comments
 (0)