diff options
author | Mateja <mail@matejamaric.com> | 2020-11-07 00:10:15 +0100 |
---|---|---|
committer | Mateja <mail@matejamaric.com> | 2020-11-07 00:10:15 +0100 |
commit | e4fec1b5386c19a7c0bfb28b850862159c8710dd (patch) | |
tree | 21b6d41bda84fc85aaab996bdc2bf1bdb76d1cca | |
parent | 7907c1eecc37e5378da0e28838203c70c693ec71 (diff) | |
download | yota-laravel-e4fec1b5386c19a7c0bfb28b850862159c8710dd.tar.gz yota-laravel-e4fec1b5386c19a7c0bfb28b850862159c8710dd.zip |
Work on gallery...
-rw-r--r-- | app/Http/Controllers/GalleryController.php | 25 | ||||
-rw-r--r-- | database/migrations/2020_11_06_185340_create_images_table.php | 2 | ||||
-rw-r--r-- | resources/views/pages/gallery-add.blade.php | 13 | ||||
-rw-r--r-- | routes/web.php | 2 |
4 files changed, 37 insertions, 5 deletions
diff --git a/app/Http/Controllers/GalleryController.php b/app/Http/Controllers/GalleryController.php index 8f42b48..3f11997 100644 --- a/app/Http/Controllers/GalleryController.php +++ b/app/Http/Controllers/GalleryController.php @@ -4,6 +4,13 @@ namespace App\Http\Controllers; use Illuminate\Http\Request; +use dd; + +use Illuminate\Support\Facades\Redirect; +use Illuminate\Support\Facades\Validator; + +use App\Models\Image; + class GalleryController extends Controller { /** @@ -34,7 +41,21 @@ class GalleryController extends Controller */ public function store(Request $request) { - // + $validatedData = $request->validate([ + 'images' => 'required' + ]); + $images = $request->file('images'); + foreach ($images as $image) { + $save = new Image(); + $save->path = 'imgs/'; + $save->name = time() . '.' . $image->getClientOriginalExtension(); + + $image->storeAs($save->path, $save->name); + + $save->save(); + } + + return Redirect::back(); } /** @@ -79,6 +100,6 @@ class GalleryController extends Controller */ public function destroy($id) { - // + return Redirect::back(); } } diff --git a/database/migrations/2020_11_06_185340_create_images_table.php b/database/migrations/2020_11_06_185340_create_images_table.php index 5c03545..98b070d 100644 --- a/database/migrations/2020_11_06_185340_create_images_table.php +++ b/database/migrations/2020_11_06_185340_create_images_table.php @@ -16,7 +16,7 @@ class CreateImagesTable extends Migration Schema::create('images', function (Blueprint $table) { $table->id(); $table->string('name'); - $table->string('path'); + $table->string('path', 255); $table->timestamps(); }); } diff --git a/resources/views/pages/gallery-add.blade.php b/resources/views/pages/gallery-add.blade.php index 2d0ec27..868f212 100644 --- a/resources/views/pages/gallery-add.blade.php +++ b/resources/views/pages/gallery-add.blade.php @@ -5,8 +5,19 @@ @section('content') <form action="{{ route('galleryAddForm')}}" method="POST" enctype="multipart/form-data"> @csrf + @if ($errors->any()) + <div class="alert alert-danger"> + <ul> + @foreach ($errors->all() as $error) + <li>{{ $error }}</li> + @endforeach + </ul> + </div> + @endif <div class="form-group"> - <input type="file" placeholder="Choose images" name="images"/> + <label for="img">Choose images:</label> + <br> + <input type="file" name="images[]" id="img" accept="image/*" multiple required> </div> <input type="submit" value="Submit" name="submit" class="btn btn-primary"/> </form> diff --git a/routes/web.php b/routes/web.php index 02b5107..fc9297e 100644 --- a/routes/web.php +++ b/routes/web.php @@ -33,7 +33,7 @@ Route::get('/news/delete/{id}', [NewsController::class, 'destroy'])->name('newsD Route::get('/gallery', [GalleryController::class, 'index'])->name('gallery'); Route::get('/gallery/add', [GalleryController::class, 'create'])->name('galleryAdd')->middleware(['auth']); -Route::post('/gallery/add', [GalleryController::class, 'create'])->name('galleryAddForm')->middleware(['auth']); +Route::post('/gallery/add', [GalleryController::class, 'store'])->name('galleryAddForm')->middleware(['auth']); Route::get('/gallery/delete/{id}', [GalleryController::class, 'destroy'])->name('galleryDelete')->middleware(['auth']); |