Skip to content

Commit d4f34e1

Browse files
authored
Merge pull request #9 from jhylmb/patch-2
`Article` 모델 삭제 시 연결된 `Attachment` 모델 및 `public/files` 디렉토리에 저장된 파일도 삭제하도록 수정
2 parents 54fdaf4 + c915399 commit d4f34e1

File tree

1 file changed

+17
-0
lines changed

1 file changed

+17
-0
lines changed

app/Http/Controllers/ArticlesController.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
88
use Illuminate\Database\Eloquent\Collection;
99
use Illuminate\Http\Request;
10+
use File;
1011

1112
class ArticlesController extends Controller implements Cacheable
1213
{
@@ -177,12 +178,28 @@ public function update(ArticlesRequest $request, Article $article)
177178
public function destroy(Article $article)
178179
{
179180
$this->authorize('delete', $article);
181+
182+
$this->deleteAttachments($article->attachments);
183+
180184
$article->delete();
181185

182186
event(new \App\Events\ModelChanged(['articles']));
183187

184188
return response()->json([], 204, [], JSON_PRETTY_PRINT);
185189
}
190+
191+
public function deleteAttachments(Collection $attachments)
192+
{
193+
$attachments->each(function ($attachment) {
194+
$filePath = $attachments_path($attachment->filename);
195+
196+
if (File::exists($filePath)) {
197+
File::delete($filePath);
198+
}
199+
200+
return $attachment->delete();
201+
});
202+
}
186203

187204
/* Response Methods */
188205

0 commit comments

Comments
 (0)