Laravel Five Basics (11)-Child view and form reuse

Source: Internet
Author: User
Laravel 5 Basics (11)-Child view and form multiplexing

We need to deal with the issue of editing articles. Of course we can add new routes manually, just like this:

Route::get('/articles/{id}/edit', '[email protected]');

Let's use it at the command artisan line route:list to view our current routes:

php artisan route:list

In the case of RESTful conditions, it resource is a good choice to use the Laravel route directly, but we remove all the routes and add only one:

Route::resource('articles', 'ArticlesController');

Using php artisan route:list View routing again, wow, a bunch of routes that match our expectations have been generated. Take a closer look at each item.

Now add the 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')    

Edit: {!! $article->title !!}

...

OK, I admit that the code is copied from the create.blade.php , modified, the question is we need to repeat it? We'll take care of this later, and now we'll look at the form's commit problem. In the route php artisan route:list , look again, modify the PATCH method we use to modify the view:

Access in the browser /articles/1/edit , look at the source code, found Laravel automatically generated _method=PATCH hidden fields.

The problem is that we edit the article, but the information in the article is not shown and we modify the view:

 {!! 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 handle it later.

Now add the 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 to verify in the process of modification, let us reuse our Request class, will be CreateArticleRequest renamed to more general ArticleRequest , do not forget to modify the store parameters in the method.

    public function update($id, Requests\ArticleRequest $request) {        $article = Article::findOrFail($id);        $article->update($request->all());        return redirect('articles');    }

The remaining problem is that our new and edited ones use most of the same code, such as displaying errors, but there are two of them, so let's change the problem.

We create a views/articles new file directly below list.blade.php and copy the error handling code from create.blade.php :

@if ($errors->any())    
  
   
   
    @foreach($errors->all() as $error)
  • {{ $error }}
  • @endforeach
@endif

create.blade.phpjust replace the error-handling code with the following statement:

@include('articles.list')

Let's deal with the form code again, except for the difference between the form code form and the Submit button. We create a view articles/form_partial.blade.php that copies the code.

    {!! Form::label('title', 'Title:') !!}    {!! Form::text('title', null, ['class' => 'form-control']) !!}    {!! Form::label('body', 'Body:') !!}    {!! Form::textarea('body', null, ['class' => 'form-control']) !!}    {!! Form::label('published_at', 'Publish On:') !!}    {!! Form::input('date', 'published_at', date('Y-m-d'), ['class' => 'form-control']) !!}    {{--这里要设置变量,依据是编辑还是修改来改变,当然也可以不放置在partial中--}}    {!! Form::submit($submitButtonText, ['class' => 'btn btn-primary form-control']) !!}

Modifycreate.blade.php

@extends('layout')@section('content')    

Write a New Article

@include('articles.list') {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open(['url' => 'articles']) !!} @include('articles.form_partial', ['submitButtonText' => 'Add Article']) {!! Form::close() !!}@stop

Modifyedit.blade.php

@extends('layout')@section('content')    

Edit: {!! $article->title !!}

@include('articles.list') {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::model($article, ['method' => 'PATCH', 'url' => 'articles/' . $article->id]) !!} @include('articles.form_partial', ['submitButtonText' => 'Update Article']) {!! Form::close() !!}@stop
  • 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.