From 8004c8d69e2975e633f2e191525201d2a0cde657 Mon Sep 17 00:00:00 2001 From: Jaiden Date: Sat, 24 May 2025 17:01:54 -0400 Subject: [PATCH] update --- app/Http/Controllers/DeletionController.php | 22 +++ app/Livewire/Posts/Comment.php | 16 ++ app/Livewire/Posts/View.php | 18 +- app/Models/Comment.php | 24 +++ app/Models/Post.php | 9 + app/Models/User.php | 6 +- config/livewire.php | 160 ++++++++++++++++++ ...025_05_24_062415_create_comments_table.php | 29 ++++ .../views/livewire/pages/upload.blade.php | 2 +- .../views/livewire/posts/comment.blade.php | 16 ++ resources/views/livewire/posts/view.blade.php | 17 +- routes/web.php | 6 + 12 files changed, 320 insertions(+), 5 deletions(-) create mode 100644 app/Http/Controllers/DeletionController.php create mode 100644 app/Livewire/Posts/Comment.php create mode 100644 app/Models/Comment.php create mode 100644 config/livewire.php create mode 100644 database/migrations/2025_05_24_062415_create_comments_table.php create mode 100644 resources/views/livewire/posts/comment.blade.php diff --git a/app/Http/Controllers/DeletionController.php b/app/Http/Controllers/DeletionController.php new file mode 100644 index 0000000..f8a09f0 --- /dev/null +++ b/app/Http/Controllers/DeletionController.php @@ -0,0 +1,22 @@ +user->id) + { + abort(401); + } + + $postid = $comment->post->id; + $comment->delete(); + return redirect("/posts/$postid"); + } +} diff --git a/app/Livewire/Posts/Comment.php b/app/Livewire/Posts/Comment.php new file mode 100644 index 0000000..ab51d53 --- /dev/null +++ b/app/Livewire/Posts/Comment.php @@ -0,0 +1,16 @@ + $this->post->comments + 'comments' => $this->post->comments->sortByDesc('created_at'), ])->title("Post {$this->post->id}"); } + + public function postComment() + { + $this->validate(); + $user = Auth::user(); + if ($comment = PostComment::create(['message' => $this->message])) + { + $this->post->comments()->save($comment); + $user->comments()->save($comment); + } + return $this->redirect("/posts/{$this->post->id}"); + } } diff --git a/app/Models/Comment.php b/app/Models/Comment.php new file mode 100644 index 0000000..7778d63 --- /dev/null +++ b/app/Models/Comment.php @@ -0,0 +1,24 @@ +belongsTo(Post::class); + } + + public function user(): BelongsTo + { + return $this->belongsTo(User::class); + } +} diff --git a/app/Models/Post.php b/app/Models/Post.php index 4a1f279..8dac764 100644 --- a/app/Models/Post.php +++ b/app/Models/Post.php @@ -5,12 +5,16 @@ namespace App\Models; use Illuminate\Support\Facades\File; use Illuminate\Support\Facades\Storage; use MongoDB\Laravel\Eloquent\Model; +use MongoDB\Laravel\Eloquent\SoftDeletes; use MongoDB\Laravel\Relations\BelongsTo; use MongoDB\Laravel\Relations\BelongsToMany; +use MongoDB\Laravel\Relations\HasMany; use Symfony\Component\HttpFoundation\StreamedResponse; class Post extends Model { + use SoftDeletes; + protected $fillable = [ 'rating', 'extension', 'featured' ]; public function user(): BelongsTo @@ -23,6 +27,11 @@ class Post extends Model return $this->belongsToMany(Tag::class); } + public function comments(): HasMany + { + return $this->hasMany(Comment::class); + } + protected function toBase64(string $path): string { $ext = $this->extension; diff --git a/app/Models/User.php b/app/Models/User.php index 294ab29..ac569a8 100644 --- a/app/Models/User.php +++ b/app/Models/User.php @@ -19,7 +19,6 @@ class User extends Authenticatable protected $fillable = [ 'name', 'email', - 'password', ]; protected $hidden = [ @@ -39,4 +38,9 @@ class User extends Authenticatable { return $this->hasMany(Post::class); } + + public function comments(): HasMany + { + return $this->hasMany(Comment::class); + } } diff --git a/config/livewire.php b/config/livewire.php new file mode 100644 index 0000000..e11c4f8 --- /dev/null +++ b/config/livewire.php @@ -0,0 +1,160 @@ + 'App\\Livewire', + + /* + |--------------------------------------------------------------------------- + | View Path + |--------------------------------------------------------------------------- + | + | This value is used to specify where Livewire component Blade templates are + | stored when running file creation commands like `artisan make:livewire`. + | It is also used if you choose to omit a component's render() method. + | + */ + + 'view_path' => resource_path('views/livewire'), + + /* + |--------------------------------------------------------------------------- + | Layout + |--------------------------------------------------------------------------- + | The view that will be used as the layout when rendering a single component + | as an entire page via `Route::get('/post/create', CreatePost::class);`. + | In this case, the view returned by CreatePost will render into $slot. + | + */ + + 'layout' => 'components.layouts.app', + + /* + |--------------------------------------------------------------------------- + | Lazy Loading Placeholder + |--------------------------------------------------------------------------- + | Livewire allows you to lazy load components that would otherwise slow down + | the initial page load. Every component can have a custom placeholder or + | you can define the default placeholder view for all components below. + | + */ + + 'lazy_placeholder' => null, + + /* + |--------------------------------------------------------------------------- + | Temporary File Uploads + |--------------------------------------------------------------------------- + | + | Livewire handles file uploads by storing uploads in a temporary directory + | before the file is stored permanently. All file uploads are directed to + | a global endpoint for temporary storage. You may configure this below: + | + */ + + 'temporary_file_upload' => [ + 'disk' => null, // Example: 'local', 's3' | Default: 'default' + 'rules' => ['required', 'file', 'max:20480'], // Example: ['file', 'mimes:png,jpg'] | Default: ['required', 'file', 'max:12288'] (12MB) + 'directory' => null, // Example: 'tmp' | Default: 'livewire-tmp' + 'middleware' => null, // Example: 'throttle:5,1' | Default: 'throttle:60,1' + 'preview_mimes' => [ // Supported file types for temporary pre-signed file URLs... + 'png', 'gif', 'bmp', 'svg', 'mp4', + 'mov', 'avi', 'wmv', 'mp3', + 'jpg', 'jpeg', 'webp', + ], + 'max_upload_time' => 5, // Max duration (in minutes) before an upload is invalidated... + 'cleanup' => true, // Should cleanup temporary uploads older than 24 hrs... + ], + + /* + |--------------------------------------------------------------------------- + | Render On Redirect + |--------------------------------------------------------------------------- + | + | This value determines if Livewire will run a component's `render()` method + | after a redirect has been triggered using something like `redirect(...)` + | Setting this to true will render the view once more before redirecting + | + */ + + 'render_on_redirect' => false, + + /* + |--------------------------------------------------------------------------- + | Eloquent Model Binding + |--------------------------------------------------------------------------- + | + | Previous versions of Livewire supported binding directly to eloquent model + | properties using wire:model by default. However, this behavior has been + | deemed too "magical" and has therefore been put under a feature flag. + | + */ + + 'legacy_model_binding' => false, + + /* + |--------------------------------------------------------------------------- + | Auto-inject Frontend Assets + |--------------------------------------------------------------------------- + | + | By default, Livewire automatically injects its JavaScript and CSS into the + | and of pages containing Livewire components. By disabling + | this behavior, you need to use @livewireStyles and @livewireScripts. + | + */ + + 'inject_assets' => true, + + /* + |--------------------------------------------------------------------------- + | Navigate (SPA mode) + |--------------------------------------------------------------------------- + | + | By adding `wire:navigate` to links in your Livewire application, Livewire + | will prevent the default link handling and instead request those pages + | via AJAX, creating an SPA-like effect. Configure this behavior here. + | + */ + + 'navigate' => [ + 'show_progress_bar' => true, + 'progress_bar_color' => '#2299dd', + ], + + /* + |--------------------------------------------------------------------------- + | HTML Morph Markers + |--------------------------------------------------------------------------- + | + | Livewire intelligently "morphs" existing HTML into the newly rendered HTML + | after each update. To make this process more reliable, Livewire injects + | "markers" into the rendered Blade surrounding @if, @class & @foreach. + | + */ + + 'inject_morph_markers' => true, + + /* + |--------------------------------------------------------------------------- + | Pagination Theme + |--------------------------------------------------------------------------- + | + | When enabling Livewire's pagination feature by using the `WithPagination` + | trait, Livewire will use Tailwind templates to render pagination views + | on the page. If you want Bootstrap CSS, you can specify: "bootstrap" + | + */ + + 'pagination_theme' => 'tailwind', +]; diff --git a/database/migrations/2025_05_24_062415_create_comments_table.php b/database/migrations/2025_05_24_062415_create_comments_table.php new file mode 100644 index 0000000..d8e828f --- /dev/null +++ b/database/migrations/2025_05_24_062415_create_comments_table.php @@ -0,0 +1,29 @@ +id(); + $table->string('message'); + $table->timestamps(); + $table->softDeletes(); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('comments'); + } +}; diff --git a/resources/views/livewire/pages/upload.blade.php b/resources/views/livewire/pages/upload.blade.php index a42e0a5..ace10b8 100644 --- a/resources/views/livewire/pages/upload.blade.php +++ b/resources/views/livewire/pages/upload.blade.php @@ -2,7 +2,7 @@

Upload

- +