Skip to content

Commit

Permalink
Feature: Carousel
Browse files Browse the repository at this point in the history
  • Loading branch information
MilesChou committed Aug 4, 2023
1 parent faf5f8f commit 3934c27
Show file tree
Hide file tree
Showing 13 changed files with 447 additions and 1 deletion.
114 changes: 114 additions & 0 deletions app/Http/Controllers/Admin/CarouselController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Carousel;
use Illuminate\Http\Request;
use RealRashid\SweetAlert\Facades\Alert;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\File;

class CarouselController extends Controller
{
public function index()
{
$carousels = Carousel::latest()->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;
}
}
}
10 changes: 9 additions & 1 deletion app/Http/Controllers/User/Shop.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -12,15 +14,21 @@
*/
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')
->take(24)
->get();

return ViewFactory::make('shop.index')->with([
'carousels' => $carousels,
'newProducts' => $randomProducts,
]);
}
Expand Down
21 changes: 21 additions & 0 deletions app/Models/Carousel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace App\Models;

use Carbon\Carbon;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property int id
* @property string img
* @property string caption
* @property Carbon created_at
* @property Carbon updated_at
*/
class Carousel extends Model
{
use HasFactory;

protected $fillable = ['caption', 'img'];
}
33 changes: 33 additions & 0 deletions database/migrations/2020_11_09_233125_create_carousels_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateCarouselsTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('carousels', function (Blueprint $table) {
$table->id();
$table->string('img');
$table->string('caption');
$table->timestamps();
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('carousels');
}
}
39 changes: 39 additions & 0 deletions database/seeders/CarouselSeeder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Carousel;

class CarouselSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$carousels = [
[
'caption' => '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']
]);
}
}
}
1 change: 1 addition & 0 deletions database/seeders/DatabaseSeeder.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public function run()
RoleSeeder::class,
UserSeeder::class,
CategorySeeder::class,
CarouselSeeder::class,
ProductSeeder::class,
]);
}
Expand Down
51 changes: 51 additions & 0 deletions resources/views/admin/carousel/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
@extends('layouts.admin')

@section('content')
<div class="container-fluid">

<div class="row mb-4">
<div class="col-sm-12 col-md-12">
<h1 class="h3 mb-0 text-gray-800">Creating a banner</h1>
<a href="{{route('admin.dashboard')}}" class="btn btn-primary btn-sm float-right">Go back</a>
</div>
</div>

<div class="row">
<div class="col-xl-12 col-md-12 mb-4">
<div class="card">
<div class="card-body">
@if($errors->any())
<div class="alert alert-danger">
{{ implode('', $errors->all(':message')) }}
</div>
@endif
<form action="{{route('carousel.store')}}" method="POST" enctype="multipart/form-data">
@csrf
<div class="form-group">
<label for="">Caption for this carousel image</label>
<input type="text" class="form-control @error('caption') is-invalid @enderror" name="caption" placeholder="Caption" >
@error('caption')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>

<div class="form-group">
<label for="exampleFormControlFile1">Example file input</label>
<input type="file" class="form-control-file" name="file" id="exampleFormControlFile1">
@error('file')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>

<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
52 changes: 52 additions & 0 deletions resources/views/admin/carousel/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
@extends('layouts.admin')

@section('content')
<div class="container-fluid">

<div class="row mb-4">
<div class="col-sm-12 col-md-12">
<h1 class="h3 mb-0 text-gray-800">Updating a banner</h1>
<a href="{{route('admin.dashboard')}}" class="btn btn-primary btn-sm float-right">Go back</a>
</div>
</div>

<div class="row">
<div class="col-xl-12 col-md-12 mb-4">
<div class="card">
<div class="card-body">
@if($errors->any())
<div class="alert alert-danger">
{{ implode('', $errors->all(':message')) }}
</div>
@endif
<form action="{{route('carousel.update',['carousel'=>$carousel])}}" method="POST" enctype="multipart/form-data">
@csrf @method('PUT')
<div class="form-group">
<label for="">Caption for this carousel image</label>
<input type="text" class="form-control @error('caption') is-invalid @enderror" name="caption" value="{{$carousel->caption}}" placeholder="Caption" >
@error('caption')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>

<div class="form-group">
<img src="{{asset($carousel->img)}}" class="img-fluid mb-4" alt="carousel image">
<label for="exampleFormControlFile1">Example file input</label>
<input type="file" class="form-control-file" name="file" id="exampleFormControlFile1">
@error('file')
<span class="invalid-feedback" role="alert">
<strong>{{ $message }}</strong>
</span>
@enderror
</div>

<button type="submit" class="btn btn-primary">Update</button>
</form>
</div>
</div>
</div>
</div>
</div>
@endsection
Loading

0 comments on commit 3934c27

Please sign in to comment.