diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 08b5e8b..fe0ac4c 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -66,19 +66,22 @@ public function store(StorePostRequest $request) /** @var \Illuminate\Http\UploadedFile[] $files */ $files = $data['attachments'] ?? []; + $attachments = []; foreach ($files as $file) { $path = $file->store('attachments/' . $post->id, 'public'); $allFilePaths[] = $path; - PostAttachment::create([ + $attachments[] = [ 'post_id' => $post->id, 'name' => $file->getClientOriginalName(), 'path' => $path, 'mime' => $file->getMimeType(), 'size' => $file->getSize(), 'created_by' => $user->id - ]); + ]; } + PostAttachment::insert($attachments); + DB::commit(); $group = $post->group; @@ -92,9 +95,7 @@ public function store(StorePostRequest $request) Notification::send($followers, new PostCreated($post, $user, null)); } catch (\Exception $e) { - foreach ($allFilePaths as $path) { - Storage::disk('public')->delete($path); - } + Storage::disk('public')->delete($allFilePaths); DB::rollBack(); throw $e; } @@ -117,35 +118,33 @@ public function update(UpdatePostRequest $request, Post $post) $deleted_ids = $data['deleted_file_ids'] ?? []; // 1, 2, 3, 4 - $attachments = PostAttachment::query() - ->where('post_id', $post->id) - ->whereIn('id', $deleted_ids) - ->get(); - - foreach ($attachments as $attachment) { - $attachment->delete(); - } + // Deleting posts attachments + PostAttachment::query() + ->where('post_id', $post->id) + ->whereIn('id', $deleted_ids) + ->delete(); /** @var \Illuminate\Http\UploadedFile[] $files */ $files = $data['attachments'] ?? []; + $attachments = []; foreach ($files as $file) { $path = $file->store('attachments/' . $post->id, 'public'); $allFilePaths[] = $path; - PostAttachment::create([ + $attachments[] = [ 'post_id' => $post->id, 'name' => $file->getClientOriginalName(), 'path' => $path, 'mime' => $file->getMimeType(), 'size' => $file->getSize(), 'created_by' => $user->id - ]); + ]; } + PostAttachment::insert($attachments); + DB::commit(); } catch (\Exception $e) { - foreach ($allFilePaths as $path) { - Storage::disk('public')->delete($path); - } + Storage::disk('public')->delete($allFilePaths); DB::rollBack(); throw $e; } diff --git a/package-lock.json b/package-lock.json index 59e5998..0f8be93 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,5 +1,5 @@ { - "name": "html", + "name": "laravel-social-media-website", "lockfileVersion": 3, "requires": true, "packages": {