Skip to content

Instantly share code, notes, and snippets.

@ryanorsinger
Created October 15, 2014 16:53
Show Gist options
  • Save ryanorsinger/b93a1bb1f411a211df07 to your computer and use it in GitHub Desktop.
Save ryanorsinger/b93a1bb1f411a211df07 to your computer and use it in GitHub Desktop.
Image Upload and associating an image to a post
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class AddImageToPosts extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('posts', function(Blueprint $table)
{
$table->string('image')->nullable();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('posts', function(Blueprint $table)
{
$table->dropColumn('image');
});
}
}
@extends('layouts.master')
@section('page-head')
<h1 class="page-header">Create a New Post</h1>
@stop
@section('content')
// Need to add 'files' => true to the attributes array on Form::open
{{ Form::open(array('action' => 'PostsController@store', 'files' => true, 'class' => 'form-horizontal')) }}
{{ Form::label('title', 'Post Title', array('class' => 'col-sm-2 control-label')) }}
{{ Form::text('title', Input::old('title'), array('class' => 'form-control')) }}
{{ Form::label('body', 'Post Body', array('class' => 'col-sm-2 control-label')) }}
{{ Form::textarea('body', Input::old('body'), array('rows' => '10', 'class' => 'form-control')) }}
{{ Form::label('image','File', array('class'=>'col-sm-2 control-label')) }}
{{ Form::file('image','',array('class'=>'form-control')) }}
{{ Form::close() }}
@stop
public function store()
{
$validator = Validator::make(Input::all(), Post::$rules);
if ($validator->fails()) {
Session::flash('errorMessage', 'Failed to save post!');
return Redirect::back()->withInput()->withErrors($validator);
} else {
$post = new Post();
$post->user_id = Auth::id();
if (Input::hasFile('image')) {
$file = Input::file('image');
$destination_path = public_path() . '/img/';
$filename = str_random(6) . '_' . $file->getClientOriginalName();
$uploadSuccess = $file->move($destination_path, $filename);
$post->image = '/img/' . $filename;
}
$post->title = Input::get('title');
$post->body = Input::get('body');
$post->save();
Session::flash('successMessage', 'Post saved!');
return Redirect::action('PostsController@show', $post->id);
}
}
// <img src="{{{ $post->image }}}"> is what we want to add into the post
@extends('layouts.master')
@section('top-script')
<style type="text/css">
article > header {
margin-bottom: 20px;
}
</style>
@stop
@section('content')
<article>
<header>
<h1>{{{ $post->title }}}</h1>
<small class="text-muted">posted by {{{ $post->user->first_name }}} {{{ $post->user->last_name }}} {{{ $post->updated_at->diffForHumans() }}}</small>
</header>
<p>{{{ $post->body }}}</p>
<img src="{{{ $post->image }}}">
</article>
{{ Form::open(array('action' => ['PostsController@destroy', $post->id], 'method' => 'delete', 'id' => 'delete-form')) }}
{{ link_to_action('PostsController@index', 'Back', [], array('class' => 'btn btn-link btn-sm')) }}
@if (Auth::check())
{{ link_to_action('PostsController@edit', 'Edit this Post', $post->id, array('class' => 'btn btn-link btn-sm')) }}
{{ Form::submit('Delete Post', array('class' => 'btn btn-danger btn-sm')) }}
@endif
{{ Form::close() }}
@stop
@section('bottom-script')
<script type="text/javascript">
$("#delete-form").submit(function(event) {
if (!confirm('Are you sure you want to delete this post?')) {
event.preventDefault();
}
});
</script>
@stop
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment