diff --git a/app/Http/Controllers/CompanyController.php b/app/Http/Controllers/CompanyController.php index 12fcb81d..191bf9c7 100644 --- a/app/Http/Controllers/CompanyController.php +++ b/app/Http/Controllers/CompanyController.php @@ -7,11 +7,16 @@ class CompanyController extends Controller { + public function create() { + return view("companies.create"); + } + public function store(Request $request) { $company = Company::create([ 'name' => $request->name, ]); + $company->addMediaFromRequest('photo')->toMediaCollection('companies'); return 'Success'; @@ -20,7 +25,8 @@ 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->getFirstMedia('companies')->getUrl(); return view('companies.show', compact('company', 'photo')); } diff --git a/app/Http/Controllers/HouseController.php b/app/Http/Controllers/HouseController.php index c330f8aa..5c3a3efd 100644 --- a/app/Http/Controllers/HouseController.php +++ b/app/Http/Controllers/HouseController.php @@ -8,6 +8,13 @@ class HouseController extends Controller { + public function create(){ + return view("house.create"); + } + + public function edit(House $house){ + return view("house.edit", compact("house")); + } public function store(Request $request) { $filename = $request->file('photo')->store('houses'); @@ -23,8 +30,8 @@ public function store(Request $request) public function update(Request $request, House $house) { $filename = $request->file('photo')->store('houses'); - - // TASK: Delete the old file from the storage + $files = Storage::files('houses'); + Storage::delete(array_filter($files, fn ($file) => $file !== $filename )); $house->update([ 'name' => $request->name, @@ -38,5 +45,6 @@ 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..1057c31a 100644 --- a/app/Http/Controllers/OfficeController.php +++ b/app/Http/Controllers/OfficeController.php @@ -14,6 +14,8 @@ public function store(Request $request) // TASK: Upload the file "photo" so it would be written as // storage/app/public/offices/[original_filename] + $request->photo->storeAs('offices', $filename, 'public'); + Office::create([ 'name' => $request->name, 'photo' => $filename, diff --git a/app/Http/Controllers/ProjectController.php b/app/Http/Controllers/ProjectController.php index 95aed4f8..1192e2f2 100644 --- a/app/Http/Controllers/ProjectController.php +++ b/app/Http/Controllers/ProjectController.php @@ -3,6 +3,7 @@ namespace App\Http\Controllers; use App\Models\Project; +use Illuminate\Validation\Rules\File; use Illuminate\Http\Request; class ProjectController extends Controller @@ -11,11 +12,14 @@ public function store(Request $request) { $request->validate([ // TASK: Write the validation rule so "logo" file would be MAX 1 megabyte + 'logo' => [ + File::image()->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->logo->getClientOriginalName(); $request->file('logo')->storeAs('logos', $filename); Project::create([ diff --git a/app/Http/Controllers/ShopController.php b/app/Http/Controllers/ShopController.php index b2c485a3..98a8f07a 100644 --- a/app/Http/Controllers/ShopController.php +++ b/app/Http/Controllers/ShopController.php @@ -4,6 +4,7 @@ use Illuminate\Http\Request; use Intervention\Image\Facades\Image; +use Storage; class ShopController extends Controller { @@ -16,6 +17,10 @@ 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 = Image::make(Storage::get('shops/'.$filename)); + $image->resize(500,500); + $image->save(storage_path('app/shops/resized-'.$filename)); + return 'Success'; } } diff --git a/app/Models/Company.php b/app/Models/Company.php index 009cf4cb..06f9badf 100644 --- a/app/Models/Company.php +++ b/app/Models/Company.php @@ -12,4 +12,8 @@ class Company extends Model implements HasMedia use HasFactory, InteractsWithMedia; protected $fillable = ['name']; + + public static function last() { + return static::all()->last(); + } } diff --git a/resources/views/companies/create.blade.php b/resources/views/companies/create.blade.php new file mode 100644 index 00000000..8d119005 --- /dev/null +++ b/resources/views/companies/create.blade.php @@ -0,0 +1,6 @@ +
\ No newline at end of file diff --git a/resources/views/companies/show.blade.php b/resources/views/companies/show.blade.php index 98e0a17c..b7ca5e6a 100644 --- a/resources/views/companies/show.blade.php +++ b/resources/views/companies/show.blade.php @@ -2,4 +2,4 @@