Skip to content

Commit

Permalink
Part 18 - Saving our updated form data to the database and fixing a f…
Browse files Browse the repository at this point in the history
…ew errors from the last video
  • Loading branch information
jacurtis committed Mar 6, 2016
1 parent 0c4cbf9 commit 28509a3
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 3 deletions.
19 changes: 18 additions & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,24 @@ public function edit($id)
*/
public function update(Request $request, $id)
{
//
// Validate the data
$this->validate($request, array(
'title' => 'required|max:255',
'body' => 'required'
));
// Save the data to the database
$post = Post::find($id);

$post->title = $request->input('title');
$post->body = $request->input('body');

$post->save();

// set flash data with success message
Session::flash('success', 'This post was successfully saved.');

// redirect with flash data to posts.show
return redirect()->route('posts.show', $post->id);
}

/**
Expand Down
4 changes: 2 additions & 2 deletions resources/views/posts/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
@section('content')

<div class="row">
{!! Form::model($post, ['route' => ['posts.update', $post->id]]) !!}
{!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
<div class="col-md-8">
{{ Form::label('title', 'Title:') }}
{{ Form::text('title', null, ["class" => 'form-control input-lg']) }}
Expand All @@ -31,7 +31,7 @@
{!! Html::linkRoute('posts.show', 'Cancel', array($post->id), array('class' => 'btn btn-danger btn-block')) !!}
</div>
<div class="col-sm-6">
{!! Html::linkRoute('posts.update', 'Save Changes', array($post->id), array('class' => 'btn btn-success btn-block')) !!}
{{ Form::submit('Save Changes', ['class' => 'btn btn-success btn-block']) }}
</div>
</div>

Expand Down

0 comments on commit 28509a3

Please sign in to comment.