forked from nishangupta/Mart
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
447 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
database/migrations/2020_11_09_233125_create_carousels_table.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'] | ||
]); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.