BTemplates.com

Advertisement

YoGiTeCh....... Powered by Blogger.

About Me

My photo
Mumbai, Maharastra, India

How to login with twitter in laravel 5.4

In this Tutorial will show you how to user login with twitter using laravel/socialite packages. social authentication is very importan...

Contact Form

Name

Email *

Message *

Search This Blog

Subscribe

Translate

Recent Posts

Posts

Pages

Blogger templates

Popular Posts

Tuesday, 28 March 2017

How to upload single image in laravel



This tutorial will show you how to upload single image in laravel 5.4 version. Laravel 5.4 provide easy and simple way to create file uploading with validation. you can easily implement this on your laravel project.

1. Create new Project:
we can create project using following command :
composer create-project --prefer-dist laravel/laravel Projectname
2. Create SImageController :
php artisan make:controller SImageController
app/Http/Controllers/SImageController.php

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Http\Requests; class SImageController extends Controller { public function singleimg() { return view('singleimg'); } public function singleimgpost(Request $request) { $this->validate($request, [ 'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048', ]); $imageName = time().'.'.$request->image->getClientOriginalExtension(); $request->image->move(public_path('images'), $imageName); return back() ->with('success','Single Image Uploaded successfully.') ->with('path',$imageName); } }
3. Create singleimg.blade.php file:
we can easily create blade.php file just click on resources then right click on views folder and create new file.

resources/views/singleimg.blade.php

<!DOCTYPE html> <html> <head> <title>Single Image Upload</title> <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> </head> <body> <nav class="navbar navbar-inverse navbar-fixed-top"> <div class="container-fluid"> <div class="navbar-header"> <a class="navbar-brand" href="/">Welcome</a> </div> <ul class="nav navbar-nav"> <li class="{{ Request::is( '/') ? 'active' : '' }}"><a href="/">Home</a></li> <li class="{{Request::is('singleimg')? 'active' : '' }}"><a href="">Single Image</a></li> </ul> </div> </nav> <div class="container"> <div class="well-primary" style="padding-top: 50px;"><h2>Dashboard</h2></div> <div class="panel panel-danger"> <div class="panel-heading"><h2 align="center">Single Image Upload</h2></div> <div class="panel-body"> @if (count($errors) > 0) <div class="alert alert-danger"> <strong>Whoops!</strong> There were some problems with your input.<br><br> <ul> @foreach ($errors->all() as $error) <li>{{ $error }}</li> @endforeach </ul> </div> @endif @if ($message = Session::get('success')) <div class="alert alert-success alert-block"> <button type="button" class="close" data-dismiss="alert">×</button> <strong>{{ $message }}</strong> </div> <div align="center"><img src="/images/{{ Session::get('path') }}"></div><br> @endif <form action="{{ url('singleimg') }}" enctype="multipart/form-data" method="POST"> {{ csrf_field() }} <div class="row" align="center"> <div class="col-md-12"> <input type="file" name="image" /><br> </div> <div class="col-md-12"> <button type="submit" class="btn btn-success">Image Upload</button> </div> </div> </form> </div> </div> </div> </body> </html>

4. Define Route :
you have to add two route in routes.php file.

Route::get('singleimg', 'SImageController@singleimg');
Route::post('singleimg', 'SImageController@singleimgpost');
Now you can run your project.

0 comments:

Post a Comment