How to upload image in Laravel 6,base64 image upload in laravel,image upload in laravel 6,upload image in laravel 5.8,upload image in laravel with validation,upload a image in laravel,upload image laravel blade,upload image by laravel,upload image code in laravel,image upload controller laravel,upload image in database laravel,image upload function in laravel

How to upload image in Laravel 6

Today In this Laravel 6 tutorial, we are going to learn how to upload image or file in laravel 6 step by step. We will create a user profile form with profile image and also we will validate profile image with Laravel validator. So let’s start step by step:-

Install Laravel

Using following commands install Laravel:-

 	composer global require laravel/installer

 	laravel new myapp
 

Create Routes

Create Route to submit the form.

  Route::get('/', 'UserProfileController@index');
 	Route::post('createuserprofile', 'UserProfileController@createuserprofile');
 

Created Registration Form

In this tutorial we will use bootstrap to create user profile form in laravel blade view. So go to your application directory app/resources/views and create file userprofile.blade.php:-


<form name="userprofile" action="{{url('createuserprofile')}}" method="post" accept-charset="utf-8" enctype="multipart/form-data">
<div class="form-group">
    <label for="email">Email address:</label>
    <input type="email" class="form-control" id="email">
  </div>
<div class="form-group">
    <label for="pwd">Password:</label>
    <input type="password" class="form-control" id="pwd">
  </div>
<div class="form-group">
    <label for="pwd">Profile Image:</label>
    <input type="file" name="profile_image" class="form-control" id="profile_image">
  </div>
<div class="checkbox">
    <label><input type="checkbox"> Remember me</label>
  </div>
  <button type="submit" class="btn btn-default">Register</button>
</form>
 

Create Model

Create a model in your app directory name userprofile.php and customize like below:-


namespace App;

use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;

class UserProfile extends Authenticatable
{
    use Notifiable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $table = 'userprofile';
    protected $fillable = [
        'email', 'password', 'profile_image',
    ];

}

Create userprofile table in your database and add following fields:-

Create Controller

Now create a controller to upload profile image and save form data in database:-


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\UserProfile;
use Image;

class UserProfileController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Contracts\Support\Renderable
     */
    public function index()
    {
        return view('userprofile');
    }
    public function createuserprofile(Request $request){

      // Validate Form Fields Using Laravel Validator

      request()->validate([
            'profile_image' => 'required|email',
            'password' => 'required',
            'profile_image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
       ]);

      $email = trim($request->email);

      $password = trim($request->password);

      if ($request->file('profile_image')) {

        $file = $request->file('profile_image');      

        $file_name = time().strtolower(trim($file->getClientOriginalName()));  

        $destinationPath = public_path('/user_profile/');

        $file->move($destinationPath, $file_name);   

      }
        $objModelUserProfile= new UserProfile();
        $objModelUserProfile->email=$email;
        $objModelUserProfile->password=$password;
        $objModelUserProfile->profile_image=$file_name;
        if($objModelUserProfile->save()){
             return redirect('userprofile')->withSuccess('User profile created successfully.');
        }    

    }
}

 

See Output

Run your development server to see the output of your application.

     php artisan serve
 

If you have found this article helpful, share this article with others. You can ask question here:-

Post Created 43

Leave a Reply

Related Posts

Begin typing your search above and press enter to search. Press ESC to cancel.

Back To Top