Laravel Tutorial Image Upload Example using Intervention with Video - Part 3

Video Tutorial Upload Image 


Full Resource Code 

Installing Image Intervention
composer require intervention/image

Class for intervention : config/app->'providers'
Intervention\Image\ImageServiceProvider::class

Class for intervention : config/app->'alises'
'Image' => Intervention\Image\Facades\Image::class

Add form Upload : resources/views/admin/create
{{ Form::label('image', 'Upload Image:') }}
{{ Form::file('image') }}

Add form Upload : resources/views/admin/edit
{{ Form::label('image', 'Update image :') }}
{{ Form::file('image') }}

Add column image in table Posts :

Model for Posts : app/post
<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
public $table = "posts";
protected $fillable = [
'title',
'body',
'image',
];
}

Change code : config/filesystems->'disks'
'local' => [
'driver' => 'local',
'root' => public_path('image/'),
],

Add class and function create for Image : app\Http\Controllers\PostsController->'store'
Class
use image

Function
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time().'.'.$image->getClientOriginalExtension();
$location = public_path('image/'.$filename);
Image::make($image)->resize(300, 100)->save($location);
$post->image = $filename;
}

Add class and function update for Image : app\Http\Controllers\PostsController->'update'

Class
use Storage;

Function
if ($request->hasFile('image')) {
$image = $request->file('image');
$filename = time().'.'.$image->getClientOriginalExtension();
$location = public_path('image/'.$filename);
Image::make($image)->resize(300, 100)->save($location);

$oldfilename = $post->image;

$post->image = $filename;

Storage::delete($oldfilename);
}

Add new table cell in index : resources/views/admin/index
//table head
<th width="100px">Image</th>

//table body
<td>
<img class="img-rounded corners" width="60px" src="{{asset('image/'.$post->image)}}" alt="">
</td>

Add image to Blog Index : resources\views\blog\index
<div class="col-sm-3">
<h2><img class="img-rounded corners" width="170px" height="150px" src="{{asset('image/'.$post->image)}}" alt=""></h2>
</div>

Add image to Blog single: resources\views\blog\single
<h2><img class="img-rounded corners" width="100%" src="{{asset('image/'.$post->image)}}" alt=""></h2>