Image Upload in Laravel

Kailash Singh

Image Upload in Laravel

Published Jan 02,2021 by Kailash Singh

0 Comment     1656 Views    

In this tutorial, i am going to teach you how to upload image using Laravel.


Step 1 : Create table in database.

CREATE TABLE `elevenstech_laravel`.`image_file` ( `id` INT NOT NULL AUTO_INCREMENT , `image` VARCHAR(300) NOT NULL , `created_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP , PRIMARY KEY (`id`)) ENGINE = InnoDB;


Step 2 : Create controller for Upload image with the help of command.

php artisan make:controller MainController

eg : 


Step 3 : Load view page in main controller.


namespace App\Http\Controllers;

use Illuminate\Http\Request;

class MainController extends Controller
    function index()
        //Load view page
    	return view('welcome');



Step 4 : Create view file (welcome.blade.php)  in view folder to create design of upload image.

<h2>Image Upload In Laravel</h2>

<!-- Show success message -->
@if($message = Session::get('msg'))
    <div class="alert alert-success alert-block">

<!-- Create form--->
<form action="{{url('/main/upload-file')}}" method="post" enctype="multipart/form-data">


    <input type="file" name="image" class="form-control">

    <input type="hidden" name="token" value="{{csrf_token()}}">

    <span style="color: red; ">@error('image'){{$message}}@enderror </span>

    <button type="submit" class="btn btn-primary">Upload</button>



Step 5 : Now create a route (web,php) for Upload file.



Step 6 : Create uploadfile function in main controller to upload image.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Redirect;
use validator;
use Auth;

class MainController extends Controller
    function index()
    	return view('welcome');

    //Here i am crating a uploadfile fuction
    function uploadfile(Request $request)
    	//Validation for image 
    	//only accept jpeg, jpg, png, gif
           'image' => 'mimes:jpeg,jpg,png,gif|required|max:1000',


        //Upload image code
	    if ($request->hasFile('image')) 
	    	//get image file
	        $image = $request->file('image');

	        //generate a new name of image
	        $imagefile = time().rand().'.'.$image->getClientOriginalExtension();

	        //Route of image file. where image is uploaded
	        $destinationPath = public_path('/image/');

	        //here we are uploading image in the folder
	        $image->move($destinationPath, $imagefile);

    	$data = array(
            'image' => $imagefile

    	//insert image in database

    	//return with success msg
    	return Redirect::back()->with('msg','File upload has been successfully.');



Step 7 :- Create Image folder to store images


Step 8 : Now use the command to run your laravel project.

eg : php artisan serve

Now, use this link to run your laravel project on browser

eg : http://localhost:8000/


Comments ( 0 )


Support Us

Subscribe My YouTube Channel

Join Our Telegram Channel & Support Eachother