Thứ Sáu, 4 tháng 11, 2016

Laravel 5 Tutorial : Simple File (Image) Upload With Example In Laravel 5.3


Laravel 5.3 Tutorial - How to upload a file (image) in Laravel 5.3? this tutorial will show you how to create simple upload image or upload file using laravel 5.3.

I assume before doing this lesson, you must read step by step How to create simple Blog in laravel 5.3.

Simple File (Image) Upload With Example

First step, we need to add new column "images" in our database "blog_post" table, so please modife your migration tables or you can change it using manually in your PhpMyAdmin.

    public function up()
    {
        Schema::create('blog_post', function (Blueprint $table) {
        $table->increments('id');
        $table->string('title');
        $table->string('description');
        $table->string('images');
        $table->timestamps();
      });
    }

Then run migration using Artisan CLI

php artisan migration:refresh

Next steps, add new folder ("image") in public\image, we will save our post images in here.

Goto our pages (blog/create.blade.php) and modife that file, we will add browse images button and save it to database
File (Image) Upload With Example In Laravel 5.3

resources\views\blog\create.blade.php

@extends('master')
@section('content')
  <h2>Add new post</h2>
  <form class="" action="/blog" method="post" enctype="multipart/form-data">
    <input type="text" name="title" value="" placeholder="this is title"><br>
    {{ ($errors->has('title')) ? $errors->first('title') : '' }} <br>
    <textarea name="description" rows="8" cols="100" placeholder="this is description"></textarea><br>
    {{ ($errors->has('description')) ? $errors->first('description') : '' }} <br>
    <input type="hidden" name="_token" value="{{ csrf_token() }}">
    <img src="http://placehold.it/100x100" id="showimages" style="max-width:200px;max-height:200px;float:left;"/>
    <div class="row">
      <div class="col-md-12">
        <input type="file" id="inputimages" name="images">
      </div>
    </div>
    <div class="col-md-12">
      <button type="submit" class="btn btn-primary pull-right" value="post">
        Submit
      </button>
    </div>
  </form>
@stop

resources\views\master.blade.php

add this JQuery files to show the thumbnail images in create pages.

<script type="text/javascript">
      function readURL(input) {
        if (input.files && input.files[0]) {
          var reader = new FileReader();
          reader.onload = function (e) {
            $('#showimages').attr('src', e.target.result);
          }
          reader.readAsDataURL(input.files[0]);
        }
      }
      $("#inputimages").change(function () {
        readURL(this);
      });
  </script>

And finally, modife our controller

BlogController.php

public function store(Request $request)
    {
        // we will create validation function here
        $this->validate($request,[
            'title'=> 'required',
            'description' => 'required',
        ]);

        $blog = new Blog;
        $blog->title = $request->title;
        $blog->description = $request->description;


        $file = $request->file('images');
        $fileName = $file->getClientOriginalName();
        $request->file('images')->move("image/",$fileName);
        $blog->images = $fileName;
        // save all data
        $blog->save();
        //redirect page after save data
        return redirect('blog')->with('message','data hasbeen updated!');
    }

Video Tutorial How to Upload file (image) in Laravel 5.3



List Video Laravel 5.3 For beginners



Build Blog using laravel 5.3



Laravel Vue.JS Tutorial



See you next lessons ...

Không có nhận xét nào:

Đăng nhận xét