From 28509a3c7aac44fb5abb70538cc84d5c65d2a636 Mon Sep 17 00:00:00 2001 From: "J. Alexander Curtis" Date: Sun, 6 Mar 2016 15:38:07 -0700 Subject: [PATCH] Part 18 - Saving our updated form data to the database and fixing a few errors from the last video --- app/Http/Controllers/PostController.php | 19 ++++++++++++++++++- resources/views/posts/edit.blade.php | 4 ++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/app/Http/Controllers/PostController.php b/app/Http/Controllers/PostController.php index 2b522af..8690ccc 100644 --- a/app/Http/Controllers/PostController.php +++ b/app/Http/Controllers/PostController.php @@ -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); } /** diff --git a/resources/views/posts/edit.blade.php b/resources/views/posts/edit.blade.php index 4652cbf..916e61c 100644 --- a/resources/views/posts/edit.blade.php +++ b/resources/views/posts/edit.blade.php @@ -5,7 +5,7 @@ @section('content')
- {!! Form::model($post, ['route' => ['posts.update', $post->id]]) !!} + {!! Form::model($post, ['route' => ['posts.update', $post->id], 'method' => 'PUT']) !!}
{{ Form::label('title', 'Title:') }} {{ Form::text('title', null, ["class" => 'form-control input-lg']) }} @@ -31,7 +31,7 @@ {!! Html::linkRoute('posts.show', 'Cancel', array($post->id), array('class' => 'btn btn-danger btn-block')) !!}
- {!! 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']) }}