We need to deal with the issue of editing articles. Of course we can add new routes by hand, just like this:
Copy Code code as follows:
Route::get ('/articles/{id}/edit ', ' articlecontroller@edit ');
Let's use Artisan's route:list at the command line to view our current route:
Copy Code code as follows:
In the case of RESTful, it may be a good choice to use the Laravel resource route directly, but we will remove all routes and add only one:
Copy Code code as follows:
Route::resource (' articles ', ' Articlescontroller ');
Again using the PHP artisan route:list to view the routing, wow, a bunch of routes that match our expectations are generated. Check each item carefully.
Now add a method to the controller:
Public function edit ($id) {
$article = Article::findorfail ($id);
Return view (' Articles.edit ', compact (' article '));
}
Create a View Now
@extends (' layout ')
@section (' content ')
Well, I admit that the code was copied from the create.blade.php, and the question is, do we need to repeat it? We'll take care of that later, and now we'll look at the submission of the form. In the routing of PHP artisan route:list, look again, modify using the PATCH method, we modify the view:
Copy Code code as follows:
{!! Form::open ([' Method ' => ' PATCH ', ' url ' => ' articles/'. $article->id])!!}
Access/articles/1/edit in the browser, look at the source code, found that Laravel automatically generated _method=patch hidden fields.
The problem is, we're editing the article, but the article doesn't show up, so let's revise the view:
Copy Code code as follows:
{!! Form::model ($article, [' Method ' => ' PATCH ', ' url ' => ' articles/'. $article->id])!!}
Ok,everything ' s OK, except that the Published_on field is still set to the current date, we'll deal with the back.
Now add a method to the controller:
Public Function Update ($id, \illuminate\http\request $request) {
$article = Article::findorfail ($id);
$article->update ($request->all ());
Return redirect (' articles ');
}
We also need validation in the process of modification, let's reuse our Request class, rename createarticlerequest to more general articlerequest, and don't forget to modify the parameters in the store method.
Public Function Update ($id, Requests\articlerequest $request) {
$article = Article::findorfail ($id);
$article->update ($request->all ());
Return redirect (' articles ');
}
The rest of the problem now is that our new and edited uses most of the same code, such as displaying errors, but there are two of them, and we're going to fix this problem.
We create a new file list.blade.php directly under Views/articles and copy the error-handling code from the create.blade.php:
@if ($errors->any ())
<ul class= "alert Alert-danger" >
@foreach ($errors->all () as $error)
<li>{{$error}}</li>
@endforeach
</ul>
@endif
In create.blade.php you simply replace the error handling code with the following statement:
Copy Code code as follows:
@include (' articles.list ')
Let's work with the form code, which differs from the submit button in addition to the form. We create a view articles/form_partial.blade.php, copy the code out
<div class= "Form-group" >
{!! Form::label (' title ', ' title: ')!!}
{!! Form::text (' title ', null, [' Class ' => ' Form-control '])!!}
</div>
<div class= "Form-group" >
{!! Form::label (' body ', ' body: ')!!}
{!! Form::textarea (' body ', null, [' Class ' => ' Form-control '])!!}
</div>
<div class= "Form-group" >
{!! Form::label (' Published_at ', ' Publish on: ')!!}
{!! Form::input (' Date ', ' Published_at ', date (' y-m-d '), [' Class ' => ' Form-control '])!!}
</div>
<div class= "Form-group" >
{--here to set the variable, depending on whether to edit or modify to change, of course, can not be placed in the partial-}}
{!! Form::submit ($submitButtonText, [' Class ' => ' btn btn-primary form-control '])!!}
</div>
Modify create.blade.php
@extends (' layout ')
@section (' content ')
Modify edit.blade.php
@extends (' layout ')
@section (' content ')
The above is the article to introduce all the content, I hope to be able to master the LARAVEL5 framework to help.