Skip to content

Commit e492285

Browse files
author
thapacodes4u
committed
Refactor: renaming the query string & also adding it to the create action
1 parent a2590b6 commit e492285

4 files changed

Lines changed: 17 additions & 14 deletions

File tree

src/Admin/Filament/Resources/PageResource.php

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
use Illuminate\Database\Eloquent\Builder;
3131
use Illuminate\Database\Eloquent\SoftDeletingScope;
3232
use Illuminate\Support\Str;
33-
use Livewire\Attributes\Url;
3433

3534
class PageResource extends Resource
3635
{
@@ -48,12 +47,6 @@ class PageResource extends Resource
4847

4948
protected static ?string $navigationLabel = 'Pages';
5049

51-
// /**
52-
// * @var array<string, mixed> | null
53-
// */
54-
// #[Url()]
55-
// public ?array $tableFilters = null;
56-
5750
public static function form(Form $form): Form
5851
{
5952
return $form
@@ -82,7 +75,8 @@ public static function form(Form $form): Form
8275
->searchable()
8376
->preload()
8477
->native(false)
85-
->columnSpan(1),
78+
->columnSpan(1)
79+
->default(request()->get('sId')),
8680

8781
TextInput::make('sef_key')
8882
->label('URL Slug')
@@ -171,8 +165,8 @@ public static function table(Table $table): Table
171165
{
172166
return $table
173167
->modifyQueryUsing(fn (Builder $query) => $query->when(
174-
request()->get('section'),
175-
fn (Builder $q, $sectionId) => $q->where('section_id', $sectionId)
168+
request()->get('sId'),
169+
fn (Builder $q, $sId) => $q->where('section_id', $sId)
176170
))
177171
->columns([
178172
TextColumn::make('title')

src/Admin/Filament/Resources/PageResource/Pages/ListPages.php

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@ class ListPages extends ListRecords
1515

1616
protected function getHeaderActions(): array
1717
{
18+
$createAction = CreateAction::make();
19+
20+
if (request()->get('sId')) {
21+
$createAction
22+
->url(fn () => PageResource::getUrl('create', [
23+
'sId' => request()->get('sId'),
24+
]));
25+
}
26+
1827
return [
19-
CreateAction::make(),
28+
$createAction,
2029
];
2130
}
2231
}

src/CmsPlugin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public function getSectionNavigationItems(): array
2828
->map(fn (Section $section): NavigationItem => NavigationItem::make($section->getTranslation('name', app()->getLocale()))
2929
->url(
3030
fn (): string => PageResource::getUrl('index', [
31-
'section' => $section->id,
31+
'sId' => $section->id,
3232
])
3333
)
3434
->icon('heroicon-o-arrow-turn-down-right')

tests/Feature/Filament/Resources/PageResourceTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@
161161
$page1 = Page::factory()->forSection($section1)->create();
162162
$page2 = Page::factory()->forSection($section2)->create();
163163

164-
$response = $this->get(PageResource::getUrl('index').'?section='.$section1->id);
164+
$response = $this->get(PageResource::getUrl('index').'?sId='.$section1->id);
165165

166166
$response->assertSuccessful();
167167
$response->assertSee($page1->title);
@@ -178,6 +178,6 @@
178178

179179
$item = $navigationItems[0];
180180
expect($item->getLabel())->toBe('Test Section');
181-
expect($item->getUrl())->toContain('section='.$section->id);
181+
expect($item->getUrl())->toContain('sId='.$section->id);
182182
expect($item->getGroup())->toBe('CMS');
183183
});

0 commit comments

Comments
 (0)