null, 'deleted' => null, ]; public function create() { $this->dispatch('post.create'); $this->modal('post-modal-form')->show(); } public function edit($id): void { $this->dispatch('post.edit', $id); $this->modal('post-modal-form')->show(); } public function delete($id): void { $post = Post::find($id); // if ($post->posts->count() > 0) { // Flux::toast(variant: 'danger', heading: 'Delete Error', text: 'Post is being used!'); // } else { $this->dispatch('show-delete-modal', [ 'id' => $id, 'type' => 'post', 'message' => 'Are you sure you want to delete ' . $post->title . '?', ]); // } } #[On('confirmed-delete')] public function handleConfirmedDelete($data): void { if ($data['type'] === 'post') { $post = Post::findOrFail($data['id']); $post->delete(); Flux::toast(variant: 'success', text: 'Post deleted'); } } public function restore($id): void { $post = Post::withTrashed()->findOrFail($id); $post->restore(); $this->resetFilters(); } public function forceDelete($id): void { $post = Post::withTrashed()->findOrFail($id); $post->forceDelete(); $this->resetFilters(); Flux::toast(variant: 'success', text: 'Post completely deleted!'); } public function resetFilters(): void { $this->filters = [ 'search' => null, 'deleted' => null, ]; } public function togglePublished($id, $published_at): void { Post::findOrFail($id) ->update(['published_at' => $published_at ? null : now() ]); } #[On('post.processed')] public function handlePostProcessed(): void { $this->modal('post-modal-form')->close(); } #[Computed] public function posts(): LengthAwarePaginator { return Post::query() ->with('category', 'tags') ->join('categories', 'posts.category_id', '=', 'categories.id') ->selectRaw('posts.*, categories.name AS category_name') ->tap(fn($query) => $this->sortBy ? $query->orderBy($this->sortBy, $this->sortDirection) : $query->orderBy('title', 'asc')) ->tap(fn($query) => $this->filters['search'] ? $query->where(function ($q) { $q->where('title', 'like', '%' . $this->filters['search'] . '%') ->orWhere('excerpt', 'like', '%' . $this->filters['search'] . '%'); }) : $query) ->tap(fn($query) => $this->handleDeletedFilter($query)) ->paginate(); } }; ?>
@lang('Posts') @lang('Manage all your posts for the blog.')
@lang('Add Post')
@lang('All') @lang('Deleted only') @lang('Active only')
@if ($this->posts->count() > 0)
@lang('Title') @lang('Category') @lang('Slug') @lang('Published') @lang('Tags') @foreach ($this->posts as $post) {{ $post->title }} {{ $post->slug }} @if ($post->published_at) @else @endif @if ($post->tags->count() > 0)
{{ $post->tags->count() }} @foreach ($post->tags as $tag) {{ $tag->name }}
@endforeach
@endif
@lang('Edit') @if (is_null($post->deleted_at)) Delete @else Restore Force delete @endif
@endforeach
@else @endif