Skip to content

Commit 0c1561a

Browse files
committed
Image upload for posts
1 parent 6abca8a commit 0c1561a

File tree

14 files changed

+229
-10
lines changed

14 files changed

+229
-10
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -3,3 +3,6 @@
33
Homestead.yaml
44
Homestead.json
55
.env
6+
public/images
7+
storage/
8+
.DS_Store

app/Http/Controllers/PostController.php

+10
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use App\Category;
1212
use Session;
1313
use Purifier;
14+
use Image;
1415

1516
class PostController extends Controller
1617
{
@@ -65,6 +66,15 @@ public function store(Request $request)
6566
$post->category_id = $request->category_id;
6667
$post->body = Purifier::clean($request->body);
6768

69+
if ($request->hasFile('featured_img')) {
70+
$image = $request->file('featured_img');
71+
$filename = time() . '.' . $image->getClientOriginalExtension();
72+
$location = public_path('images/' . $filename);
73+
Image::make($image)->resize(800, 400)->save($location);
74+
75+
$post->image = $filename;
76+
}
77+
6878
$post->save();
6979

7080
$post->tags()->sync($request->tags, false);

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
"php": ">=5.5.9",
99
"laravel/framework": "5.2.*",
1010
"laravelcollective/html": "5.2.*",
11-
"mews/purifier": "^2.0"
11+
"mews/purifier": "^2.0",
12+
"intervention/image": "^2.3"
1213
},
1314
"require-dev": {
1415
"fzaninotto/faker": "~1.4",

composer.lock

+172-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

config/app.php

+3
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,8 @@
158158
App\Providers\EventServiceProvider::class,
159159
App\Providers\RouteServiceProvider::class,
160160

161+
Intervention\Image\ImageServiceProvider::class,
162+
161163
],
162164

163165
/*
@@ -206,6 +208,7 @@
206208
'Form' => Collective\Html\FormFacade::class,
207209
'Html' => Collective\Html\HtmlFacade::class,
208210
'Purifier' => Mews\Purifier\Facades\Purifier::class,
211+
'Image' => Intervention\Image\Facades\Image::class
209212
],
210213

211214
];
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<?php
2+
3+
use Illuminate\Database\Schema\Blueprint;
4+
use Illuminate\Database\Migrations\Migration;
5+
6+
class AddImageColToPosts extends Migration
7+
{
8+
/**
9+
* Run the migrations.
10+
*
11+
* @return void
12+
*/
13+
public function up()
14+
{
15+
Schema::table('posts', function (Blueprint $table) {
16+
$table->string('image')->nullable()->after('slug');
17+
});
18+
}
19+
20+
/**
21+
* Reverse the migrations.
22+
*
23+
* @return void
24+
*/
25+
public function down()
26+
{
27+
Schema::table('posts', function (Blueprint $table) {
28+
$table->dropColumn('image');
29+
});
30+
}
31+
}

resources/views/blog/single.blade.php

+5-4
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
<div class="row">
88
<div class="col-md-8 col-md-offset-2">
9+
<img src="{{asset('/images/' . $post->image)}}" width="800" height="400" />
910
<h1>{{ $post->title }}</h1>
1011
<p>{!! $post->body !!}</p>
1112
<hr>
@@ -25,13 +26,13 @@
2526
<h4>{{ $comment->name }}</h4>
2627
<p class="author-time">{{ date('F nS, Y - g:iA' ,strtotime($comment->created_at)) }}</p>
2728
</div>
28-
29+
2930
</div>
3031

3132
<div class="comment-content">
3233
{{ $comment->comment }}
3334
</div>
34-
35+
3536
</div>
3637
@endforeach
3738
</div>
@@ -40,7 +41,7 @@
4041
<div class="row">
4142
<div id="comment-form" class="col-md-8 col-md-offset-2" style="margin-top: 50px;">
4243
{{ Form::open(['route' => ['comments.store', $post->id], 'method' => 'POST']) }}
43-
44+
4445
<div class="row">
4546
<div class="col-md-6">
4647
{{ Form::label('name', "Name:") }}
@@ -64,4 +65,4 @@
6465
</div>
6566
</div>
6667

67-
@endsection
68+
@endsection

resources/views/posts/create.blade.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
<div class="col-md-8 col-md-offset-2">
2525
<h1>Create New Post</h1>
2626
<hr>
27-
28-
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '')) !!}
27+
{!! Form::open(array('route' => 'posts.store', 'data-parsley-validate' => '', 'files' => true)) !!}
2928
{{ Form::label('title', 'Title:') }}
3029
{{ Form::text('title', null, array('class' => 'form-control', 'required' => '', 'maxlength' => '255')) }}
3130

@@ -49,7 +48,8 @@
4948

5049
</select>
5150

52-
51+
{{ Form::label('featured_img', 'Upload a Featured Image') }}
52+
{{ Form::file('featured_img') }}
5353

5454
{{ Form::label('body', "Post Body:") }}
5555
{{ Form::textarea('body', null, array('class' => 'form-control')) }}

storage/app/.gitignore

100644100755
File mode changed.

storage/framework/.gitignore

100644100755
File mode changed.

storage/framework/cache/.gitignore

100644100755
File mode changed.

storage/framework/sessions/.gitignore

100644100755
File mode changed.

storage/framework/views/.gitignore

100644100755
File mode changed.

storage/logs/.gitignore

100644100755
File mode changed.

0 commit comments

Comments
 (0)