Laravel5 series tutorial 10: modifying articles

Source: Internet
Author: User
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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.