-
+
+
+
+
diff --git a/app/Http/Controllers/Admin/CarouselController.php b/app/Http/Controllers/Admin/CarouselController.php new file mode 100644 index 00000000..27c1307b --- /dev/null +++ b/app/Http/Controllers/Admin/CarouselController.php @@ -0,0 +1,114 @@ +paginate(15); + return view('admin.carousel.index', compact('carousels')); + } + + public function create() + { + return view('admin.carousel.create'); + } + + public function store(Request $request) + { + $request->validate([ + 'caption' => 'required|min:3', + 'file' => 'required|image|max:5000' + ]); + + if ($this->saveCarousel($request)) { + Alert::toast('Carousel created!', 'success'); + } else { + Alert::toast('Something went wrong!'); + } + return redirect(route('carousel.index')); + } + + public function edit(Carousel $carousel) + { + return view('admin.carousel.edit', compact('carousel')); + } + + public function update(Request $request, Carousel $carousel) + { + $request->validate([ + 'caption' => 'required|min:3', + 'file' => 'sometimes|image|max:5000' + ]); + + if ($this->updateCarousel($request, $carousel)) { + Alert::toast('Carousel Updated!', 'success'); + } else { + Alert::toast('Something went wrong!'); + } + return redirect(route('carousel.index')); + } + + + public function destroy(Carousel $carousel) + { + File::delete(public_path($carousel->img)); + $carousel->delete(); + + Alert::toast('Carousel deleted!', 'success'); + return redirect(route('carousel.index')); + } + + private function updateCarousel($request, $carousel) + { + if ($request->hasFile('file')) { + $basename = Str::random(); + $original = $basename . '.' . $request->file->getClientOriginalExtension(); + + + //delete old image + File::delete([ + public_path($carousel->img), + ]); + + if ( + $carousel->update([ + 'caption' => $request->caption, + 'img' => '/images/banner/' . $original, + ]) + ) { + $request->file->move(public_path('/images/banner'), $original); + return true; + } + } else { + $carousel->caption = $request->caption; + $carousel->save(); + return true; + } + } + + private function saveCarousel($request) + { + //setting file upload + $basename = Str::random(); + $original = $basename . '.' . $request->file->getClientOriginalExtension(); + + if ( + Carousel::create([ + 'caption' => $request->caption, + 'img' => '/images/banner/' . $original, + ]) + ) { + $request->file->move(public_path('/images/banner'), $original); + return true; + } + } +} diff --git a/app/Http/Controllers/User/Shop.php b/app/Http/Controllers/User/Shop.php index 4f1c476b..f28ebfdf 100644 --- a/app/Http/Controllers/User/Shop.php +++ b/app/Http/Controllers/User/Shop.php @@ -3,6 +3,8 @@ namespace App\Http\Controllers\User; use App\Http\Controllers\Controller; +use App\Models\Carousel; +use App\Models\FlashSale; use App\Models\Product; use Illuminate\Contracts\View\View; use Illuminate\Support\Facades\View as ViewFactory; @@ -12,8 +14,13 @@ */ class Shop extends Controller { - public function __invoke(Product $product): View + public function __invoke(Carousel $carousel, Product $product): View { + $carousels = $carousel->newQuery() + ->latest() + ->take(3) + ->get(); + $randomProducts = $product->newQuery() ->inRandomOrder() ->with('productImage') @@ -21,6 +28,7 @@ public function __invoke(Product $product): View ->get(); return ViewFactory::make('shop.index')->with([ + 'carousels' => $carousels, 'newProducts' => $randomProducts, ]); } diff --git a/app/Models/Carousel.php b/app/Models/Carousel.php new file mode 100644 index 00000000..e5b5f2be --- /dev/null +++ b/app/Models/Carousel.php @@ -0,0 +1,21 @@ +id(); + $table->string('img'); + $table->string('caption'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('carousels'); + } +} diff --git a/database/seeders/CarouselSeeder.php b/database/seeders/CarouselSeeder.php new file mode 100644 index 00000000..bec06b2e --- /dev/null +++ b/database/seeders/CarouselSeeder.php @@ -0,0 +1,39 @@ + 'This is Caption1', + 'img' => '/images/banner/1.webp' + ], + [ + 'caption' => 'This is Caption2', + 'img' => '/images/banner/2.webp' + ], + [ + 'caption' => 'This is Caption3', + 'img' => '/images/banner/3.webp' + ] + ]; + + foreach ($carousels as $item) { + Carousel::create([ + 'caption' => $item['caption'], + 'img' => $item['img'] + ]); + } + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 235535cd..13b78b7d 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,6 +17,7 @@ public function run() RoleSeeder::class, UserSeeder::class, CategorySeeder::class, + CarouselSeeder::class, ProductSeeder::class, ]); } diff --git a/resources/views/admin/carousel/create.blade.php b/resources/views/admin/carousel/create.blade.php new file mode 100644 index 00000000..a2fca9f8 --- /dev/null +++ b/resources/views/admin/carousel/create.blade.php @@ -0,0 +1,51 @@ +@extends('layouts.admin') + +@section('content') +
# | +Caption | +Image | +Created At | +Actions | +
---|---|---|---|---|
{{$carousel->id}} | +{{$carousel->caption}} | ++ | {{$carousel->created_at->diffForHumans()}} | ++ Edit + + | +No carousel found. Create one now | ++ | + | + | + | + @endif + + |