Home Backend Development PHP Tutorial Analysis of subviews and form reuse in Laravel5 framework

Analysis of subviews and form reuse in Laravel5 framework

Jun 13, 2018 pm 04:51 PM
laravel framework php framework laravel

This article mainly introduces the use of subviews and form reuse in the Laravel5 framework. It is very detailed and comprehensive. It is very helpful for everyone to master the Laravel5 framework. Friends who need it can refer to it

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

Route::get('/articles/{id}/edit', 'ArticleController@edit');
Copy after login

Let us use artisan’s route:list on the command line to view our current routes:

php artisan route:list
Copy after login

In line with RESTful In this case, it may be a good choice to directly use laravel's resource route. Then we will remove all the routes and only add the only one:

Route::resource('articles', 'ArticlesController');
Copy after login

Use php artisan route:list again to view the route, wow , a bunch of routes that meet our expectations are generated. Look at each item carefully.

Now add method in controller:

  public function edit($id) {
    $article = Article::findOrFail($id);

    return view('articles.edit', compact('article'));
  }
Copy after login

Now create view

@extends('layout')

@section('content')
  <h1>Edit: {!! $article->title !!} </h1>

  <hr/>

  ...
Copy after login

Okay, I admit that these codes were copied from create.blade.php and modified a bit. The question is do we need to repeat them? We will deal with this issue later, now let’s look at the form submission issue. In the routing, php artisan route:list, read it again, the modification uses the PATCH method, let's modify the view:

{!! Form::open([&#39;method&#39; => &#39;PATCH&#39;, &#39;url&#39; => &#39;articles/&#39; . $article->id]) !!}
Copy after login

Visit /articles/1/edit in the browser, check the source code, and find laravel The hidden field of _method=PATCH is automatically generated.

The first problem is that we edit the article, but the article information is not displayed. Let's modify the view:

 {!! Form::model($article, [&#39;method&#39; => &#39;PATCH&#39;, &#39;url&#39; => &#39;articles/&#39; . $article->id]) !!}
Copy after login

OK, everything's ok, except that the published_on field is still set to the current date, and then We'll handle it.

Now add the method in the controller:

  public function update($id, \Illuminate\Http\Request $request) {
    $article = Article::findOrFail($id);
    $article->update($request->all());

    return redirect(&#39;articles&#39;);
  }
Copy after login

We also need to verify during the modification process, let us reuse our Request class, rename CreateArticleRequest to the 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(&#39;articles&#39;);
  }
Copy after login

The remaining problem now is that our new and edit use most of the same code, such as displaying errors, but they exist in two copies, let's Modify this question.

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

@if ($errors->any())
  <ul class="alert alert-danger">
    @foreach($errors->all() as $error)
      <li>{{ $error }}</li>
    @endforeach
  </ul>
@endif
Copy after login

In create.blade.php, just replace the error handling code with the following statement:

@include(&#39;articles.list&#39;)
Copy after login

Let's deal with the form code again. The form code is different except that the form is different from the submit button. , others are almost the same. We create a view articles/form_partial.blade.php and copy the code

<p class="form-group">
  {!! Form::label(&#39;title&#39;, &#39;Title:&#39;) !!}
  {!! Form::text(&#39;title&#39;, null, [&#39;class&#39; => &#39;form-control&#39;]) !!}
</p>

<p class="form-group">
  {!! Form::label(&#39;body&#39;, &#39;Body:&#39;) !!}
  {!! Form::textarea(&#39;body&#39;, null, [&#39;class&#39; => &#39;form-control&#39;]) !!}
</p>

<p class="form-group">
  {!! Form::label(&#39;published_at&#39;, &#39;Publish On:&#39;) !!}
  {!! Form::input(&#39;date&#39;, &#39;published_at&#39;, date(&#39;Y-m-d&#39;), [&#39;class&#39; => &#39;form-control&#39;]) !!}
</p>

<p class="form-group">
  {{--这里要设置变量,依据是编辑还是修改来改变,当然也可以不放置在partial中--}}
  {!! Form::submit($submitButtonText, [&#39;class&#39; => &#39;btn btn-primary form-control&#39;]) !!}
</p>
Copy after login

Modify create.blade.php

@extends('layout')

@section('content')
  

Write a New Article


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

Modify edit.blade.php

@extends('layout')

@section('content')
  

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


@include(&#39;articles.list&#39;) {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::model($article, [&#39;method&#39; => &#39;PATCH&#39;, &#39;url&#39; => &#39;articles/&#39; . $article->id]) !!} @include('articles.form_partial', ['submitButtonText' => 'Update Article']) {!! Form::close() !!} @stop
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone’s study , please pay attention to the PHP Chinese website for more related content!

Related recommendations:

Pages and Form Validation of Laravel 4

About Laravel framework database CURD operations and coherent operations Analysis

About the method of PHP framework Laravel plug-in Pagination to implement custom paging

The above is the detailed content of Analysis of subviews and form reuse in Laravel5 framework. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to use model events (Model Events) in Laravel framework How to use model events (Model Events) in Laravel framework Jul 28, 2023 am 10:49 AM

How to use model events (ModelEvents) in Laravel framework Laravel framework provides many powerful features, one of which is model events (ModelEvents). Model events are a feature used in Laravel's EloquentORM (Object Relational Mapping) that allows developers to execute custom code when a specific action occurs on the model. In this article, we will explore how to use model events in the Laravel framework and provide a

How to use Task Scheduler to execute scheduled tasks in the Laravel framework How to use Task Scheduler to execute scheduled tasks in the Laravel framework Jul 29, 2023 am 09:54 AM

How to use the task scheduler (TaskScheduler) to execute scheduled tasks in the Laravel framework. With the development of web applications, scheduled tasks play a crucial role in many scenarios. The Laravel framework provides a powerful task scheduler (TaskScheduler) function that can easily perform various scheduled tasks, such as generating reports, cleaning caches, sending emails, etc. This article will introduce how to use the task scheduler to execute scheduled tasks in the Laravel framework.

Use the PHP framework Laravel to develop an efficient blog system and provide high-quality blog resources Use the PHP framework Laravel to develop an efficient blog system and provide high-quality blog resources Jun 27, 2023 am 09:37 AM

With the popularization of the Internet, traditional writing methods are gradually being replaced, and people tend to write, read, and communicate through online platforms. In this information-centered era, it is very necessary to develop an efficient blog system and provide readers with high-quality blog resources. This article will introduce how to use the PHP framework Laravel to develop an efficient blog system. 1. Why choose Laravel framework? Laravel is an excellent PHP framework. It is simple, flexible, efficient and can reduce development time.

Develop efficient web applications using the Laravel framework Develop efficient web applications using the Laravel framework May 27, 2023 am 08:51 AM

With the rapid development of the Internet, Web applications are playing an increasingly important role in our lives. For developers, how to use efficient tools and frameworks to develop web applications is crucial. The Laravel framework is undoubtedly one of the efficient choices. This article will introduce the basic concepts and uses of the Laravel framework to help you quickly develop efficient web applications. 1. Basic concepts of the Laravel framework The Laravel framework is an open source web application framework based on the PHP language. it

How to use Queue function in Laravel framework How to use Queue function in Laravel framework Jul 28, 2023 pm 09:37 PM

How to use the queue (Queue) function in the Laravel framework Introduction: Queue (Queue) is a common asynchronous processing mechanism that plays an important role in web development. The Laravel framework provides powerful queue functions that can easily handle various background tasks, such as sending emails, generating reports, processing big data, etc. This article will introduce how to use the queue function in the Laravel framework, including queue configuration, task definition and execution, etc., and give corresponding code examples. 1. Configure the queue in

How to learn Laravel How to learn Laravel for free How to learn Laravel How to learn Laravel for free Apr 18, 2025 pm 12:51 PM

Want to learn the Laravel framework, but suffer from no resources or economic pressure? This article provides you with free learning of Laravel, teaching you how to use resources such as online platforms, documents and community forums to lay a solid foundation for your PHP development journey from getting started to master.

Laravel framework skills sharing Laravel framework skills sharing Apr 18, 2025 pm 01:12 PM

In this era of continuous technological advancement, mastering advanced frameworks is crucial for modern programmers. This article will help you improve your development skills by sharing little-known techniques in the Laravel framework. Known for its elegant syntax and a wide range of features, this article will dig into its powerful features and provide practical tips and tricks to help you create efficient and maintainable web applications.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

See all articles