Laravel5 series tutorial 10: modifying articles
From https://laravist.com/article/20
Laravist is the Laravel community I just launched. if you have any questions related to Laravel, please come here and ask me. I will try my best to solve the problem. later I will try to record some video tutorials, this is probably the form
Https://laravist.com/lesson/2
We have integrated a series of previous articles to implement the whole process of publishing an article. But what if we need to modify the content of an article? How can we implement it? In fact, after creating an article, the idea of editing (updating) the article is similar. The knowledge points worth noting here are Form: model and getAttribute. To see the specific steps:
Register a route
In routes. php, register the route on our editing page:
Route::get('article/edit/{id}','ArticleController@edit');
This route accepts a parameter: id, indicating the article id. we need to query the article to be modified based on this id.
Compile the edit method
Based on the route, we add the edit () method in ArticleController:
public function edit($id) { $article = Article::findOrFail($id); $tags = Tag::lists('name', 'id'); return view('articles.edit',compact('article','tags')); }
Familiar with this, we first query the articles we need to edit based on the id. for $ tags, we use the same method as create () to get our $ tags list. Render the view and pass the $ article and $ tags queried to the view.
Create View
The edit () method above specifies the rendering articles. edit (resources/views/articles/edit. blade. php) this view is created here. For convenience, we can directly create. blade. copy the php view file:
@ Extends ('app') @ section ('content') modify the article: {$ article-> title }}{!! Form: model ($ article, ['URL' => 'Article/update'])!} {!! Form: hidden ('id', $ article-> id )!!}{!! Form: label ('title', 'Title :')!!} {!! Form: text ('title', $ article-> title, ['class' => 'form-control'])!}
{!! Form: label ('content', 'body :')!!} {!! Form: textarea ('content', $ article-> content, ['class' => 'form-control'])!}
{!! Form: label ('Ed Hed _ at', 'publication date ')!!} {!! Form: input ('date', 'hhed _ at', $ article-> published_at-> format ('Y-m-D '), ['class' => 'form-control'])!}
{!! Form: label ('tag _ list', 'Select tag ')!!} {!! Form: select ('tag _ list [] ', $ tags, null, ['class' => 'form-control js-example-basic-multiple ', 'multiple '=> 'multiple'])!}
{!! Form: submit ('modify the article ', ['class' => 'btn btn-success form-control'])!}
{!! Form: close ()!!} @ If ($ errors-> any ())
@ Foreach ($ errors-> all () as $ error)
- {$ Error }}
@ Endforeach
@ Endif