From c7d0a019ff5ea306e7a893ad4efb9c2be7486836 Mon Sep 17 00:00:00 2001 From: Eden-Tran Date: Tue, 9 Apr 2024 22:14:25 +0700 Subject: [PATCH] finish all --- app/Http/Controllers/CompanyController.php | 4 +- app/Http/Controllers/HouseController.php | 63 +++++++++++----------- app/Http/Controllers/OfficeController.php | 3 +- app/Http/Controllers/ProjectController.php | 6 ++- app/Http/Controllers/ShopController.php | 1 + 5 files changed, 39 insertions(+), 38 deletions(-) diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 12fcb81d..2eda01e0 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -20,9 +20,7 @@ public function store(Request $request) public function show(Company $company) { // TASK: retrieve the full URL to the uploaded photo file, using Spatie Media Library - $photo = '???'; - + $photo = $company->getMedia('companies')->first()->getFullUrl(); return view('companies.show', compact('company', 'photo')); } - } diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index c330f8aa..b371cc6a 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -8,35 +8,36 @@ class HouseController extends Controller { - public function store(Request $request) - { - $filename = $request->file('photo')->store('houses'); - - House::create([ - 'name' => $request->name, - 'photo' => $filename, - ]); - - return 'Success'; - } - - public function update(Request $request, House $house) - { - $filename = $request->file('photo')->store('houses'); - - // TASK: Delete the old file from the storage - - $house->update([ - 'name' => $request->name, - 'photo' => $filename, - ]); - - return 'Success'; - } - - public function download(House $house) - { - // TASK: Return the $house->photo file from "storage/app/houses" folder - // for download in browser - } + public function store(Request $request) + { + $filename = $request->file('photo')->store('houses'); + + House::create([ + 'name' => $request->name, + 'photo' => $filename, + ]); + + return 'Success'; + } + + public function update(Request $request, House $house) + { + $filename = $request->file('photo')->store('houses'); + + // TASK: Delete the old file from the storage + Storage::delete($house->photo); + $house->update([ + 'name' => $request->name, + 'photo' => $filename, + ]); + + return 'Success'; + } + + public function download(House $house) + { + // TASK: Return the $house->photo file from "storage/app/houses" folder + // for download in browser + return Storage::download($house->photo); + } } diff --git a/app/Http/Controllers/OfficeController.php b/app/Http/Controllers/OfficeController.php index fae443fa..b3f25811 100644 --- a/app/Http/Controllers/OfficeController.php +++ b/app/Http/Controllers/OfficeController.php @@ -13,7 +13,7 @@ public function store(Request $request) // TASK: Upload the file "photo" so it would be written as // storage/app/public/offices/[original_filename] - + $request->file('photo')->storeAs('offices', $filename, 'public'); Office::create([ 'name' => $request->name, 'photo' => $filename, @@ -26,5 +26,4 @@ public function show(Office $office) { return view('offices.show', compact('office')); } - } diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 95aed4f8..6a4111b6 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -11,13 +11,15 @@ public function store(Request $request) { $request->validate([ // TASK: Write the validation rule so "logo" file would be MAX 1 megabyte + 'logo' => ['file', 'max:1024'] ]); // TASK: change the below line so that $filename would contain only filename // The same filename as the original uploaded file - $filename = '???'; + $filename = $request->file('logo')->getClientOriginalName(); $request->file('logo')->storeAs('logos', $filename); + Project::create([ 'name' => $request->name, 'logo' => $filename, @@ -25,4 +27,4 @@ public function store(Request $request) return 'Success'; } -} +} \ No newline at end of file diff --git a/app/Http/Controllers/ShopController.php b/app/Http/Controllers/ShopController.php index b2c485a3..20f759df 100644 --- a/app/Http/Controllers/ShopController.php +++ b/app/Http/Controllers/ShopController.php @@ -16,6 +16,7 @@ public function store(Request $request) // to size of 500x500 and store it as /storage/app/shops/resized-$filename // Use intervention/image package, it's already pre-installed for you + Image::make(storage_path('app/shops/' . $filename))->resize(500, 500)->save(storage_path('app/shops/resized-' . $filename)); return 'Success'; } }