Skip to content

Commit

Permalink
fix articles
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 24, 2023
1 parent 59ded3b commit e446dd9
Show file tree
Hide file tree
Showing 10 changed files with 509 additions and 535 deletions.
26 changes: 15 additions & 11 deletions app/Filament/Resources/Articles/ArticleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ public static function getPluralModelLabel(): string
{
return __('article.label.plural');
}
protected static function getNavigationBadge(): ?string
{
return (string) Article::count();
}

public static function form(Form $form): Form
{
Expand Down Expand Up @@ -98,7 +102,7 @@ public static function form(Form $form): Form
->schema([
SpatieMediaLibraryFileUpload::make('cover')
->label(__('article.cover_image'))
->collection('cover')
->collection('preview')
->required()
->image()
->maxFiles(1),
Expand All @@ -107,12 +111,12 @@ public static function form(Form $form): Form
Card::make()
->schema([
SpatieMediaLibraryFileUpload::make('gallery')
->label(__('article.gallery'))
->collection('gallery')
->label(__('article.gallery'))
->image()
->multiple()
->enableReordering()
->columnSpanFull(),
->multiple()
->maxFiles(20),
]),
]),

Expand All @@ -139,6 +143,12 @@ public static function table(Table $table): Table
{
return $table
->columns([
TextColumn::make('id')
->formatStateUsing(function (Article $record) {
return sprintf('#%d', $record->id);
})
->label(__('volunteer.column.id'))
->sortable(),
TextColumn::make('title')
->label(__('article.title'))
->searchable(),
Expand All @@ -161,18 +171,12 @@ public static function table(Table $table): Table
->actions([
Tables\Actions\EditAction::make(),
])
->filtersLayout(Layout::AboveContent)
->defaultSort('id', 'desc')
->bulkActions([
Tables\Actions\DeleteBulkAction::make(),
]);
}

public static function getRelations(): array
{
return [
//
];
}

public static function getPages(): array
{
Expand Down
5 changes: 5 additions & 0 deletions app/Filament/Resources/Articles/CategoryResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ public static function getPluralModelLabel(): string
return __('article.category.label.plural');
}

protected static function getNavigationBadge(): ?string
{
return (string) ArticleCategory::count();
}

public static function form(Form $form): Form
{
return $form
Expand Down
14 changes: 11 additions & 3 deletions app/Http/Controllers/ArticleController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,16 @@ public function index(Request $request): Response
'categories' => $this->getArticleCategories(),
'collection' => ArticleCardResource::collection(
Article::query()
->orderByDesc('id')
->wherePublished()
->paginate(5)
),
'topArticles' => ArticleCardResource::collection(
Article::query()
->wherePublished()
->inRandomOrder()
->limit(3)
->get())
]);
}

Expand All @@ -35,6 +42,7 @@ public function category(ArticleCategory $category, Request $request): Response
Article::query()
->whereBelongsTo($category, 'category')
->wherePublished()
->orderByDesc('id')
->paginate(5)
),
]);
Expand All @@ -44,13 +52,13 @@ public function show(Article $article): Response
{
return Inertia::render('Public/Articles/Show', [
'resource' => ArticleResource::make($article),
'related' => [], /* Article::query()
'related' => ArticleCardResource::collection(Article::query()
->wherePublished()
->whereBelongsTo('category', $article->category)
->whereBelongsTo($article->category, 'category')
->whereNot('id', $article->id)
->inRandomOrder()
->limit(3)
->get(), */
->get()),
]);
}
}
10 changes: 9 additions & 1 deletion app/Http/Resources/Articles/ArticleResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,16 @@ public function toArray(Request $request): array
'content' => $this->content,
'author' => $this->author,
'category' => $this->category?->name,
'cover' => $this->getFirstMediaUrl('cover', 'large'),
'cover' => $this->getFirstMediaUrl('preview', 'large'),
'gallery' => $this->getMedia('gallery')->map(fn (Media $media) => $media->getUrl('large')),
'swipe_gallery' => $this->getMedia('gallery')->map(function ($media) {
return [
'src' => $media->getFullUrl('large'),
'thumbnail' => $media->getFullUrl('thumb'),
'w' => 1200,
'h' => 600,
];
})->toArray(),
];
}
}
3 changes: 1 addition & 2 deletions app/Models/Article.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class Article extends Model implements HasMedia

public function registerMediaCollections(): void
{
$this->addMediaCollection('cover')
$this->addMediaCollection('preview')
->useFallbackUrl(Vite::image('placeholder.png'))
->singleFile()
->registerMediaConversions(function (Media $media) {
Expand All @@ -63,7 +63,6 @@ public function registerMediaCollections(): void

$this->addMediaCollection('gallery')
->useFallbackUrl(Vite::image('placeholder.png'))
->singleFile()
->registerMediaConversions(function (Media $media) {
$this
->addMediaConversion('thumb')
Expand Down
4 changes: 2 additions & 2 deletions app/Models/BCRProject.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ class BCRProject extends Model implements HasMedia
'is_national' => 'boolean',
];

protected $with =[
'county','category'
protected $with = [
'county', 'category',
];

public function registerMediaCollections(): void
Expand Down
Loading

0 comments on commit e446dd9

Please sign in to comment.