laravel gallery managements – How to upload image in Laravel and store in database?

In this laravel gallery managements posts, We are going to share with you how to make bootstrap laravel gallery managements with validation and fancybox in laravel 9, laravel 8, laravel 7, laravel 6 and laravel 5 web application.

laravel gallery managements – Image Gallery CRUD example from scratch

In this laravel gallery managements example i created “gallery_managements” table with main ‘title’ and ‘image’ columns. We created single controller and view file for display form and error messages, validation etc. And then complete this example you will display layout:

Step 1 : Install Laravel Application

composer create-project --prefer-dist laravel/laravel blog

Step 2: Database Configuration

.env

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=gallery_art
DB_USERNAME=root
DB_PASSWORD=Pakainfo_w3@#$%c60!@#$00025@$%^

Step 3: Create GalleryManagements Table and Model

php artisan make:migration create_gallery_managements_table
increments('id');
            $table->string('title');
            $table->string('image');
            $table->timestamps();
        });
    }


    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('gallery_managements');
    }
}
php artisan make:model GalleryManagements

app/GalleryManagements.php


Step 4: Create Route

routes/web.php

Route::get('gallery-managements', 'GalleryManagementsController@index');
Route::post('gallery-managements', 'GalleryManagementsController@upload');
Route::delete('gallery-managements/{id}', 'GalleryManagementsController@destroy');

Step 5: Create Controller

php artisan make:controller GalleryManagementsController

So, let's copy bellow code and put on GalleryManagementsController.php file.
app/Http/Controllers/ContactUSController.php

validate($request, [
    		'title' => 'required',
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);


        $input['image'] = time().'.'.$request->image->getClientOriginalExtension();
        $request->image->move(public_path('images'), $input['image']);


        $input['title'] = $request->title;
        GalleryManagements::create($input);


    	return back()
    		->with('success','Image Uploaded successfully.');
    }


    /**
     * Remove Image function
     *
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
    	GalleryManagements::find($id)->delete();
    	return back()
    		->with('success','Image removed successfully.');	
    }
}

Step 6: Create View

resources/views/gallery-managements.blade.php




    laravel gallery managements - www.pakainfo.com
    
    
    
    


    




Laravel - Image Gallery CRUD Example

{!! csrf_field() !!} @if (count($errors) > 0)
Whoops! There were some problems with your input.

    @foreach ($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif @if ($message = Session::get('success'))
{{ $message }}
@endif
Title:
Image:

Now you can open bellow URL on your browser and run laravel gallery managements:

run this Example : https://laravel.com/docs/9.x/validation#rule-image

http://localhost:8000/gallery-managements

Leave a Comment