


Laravel 5 framework learning form, laravel5 framework form_PHP tutorial
Laravel 5 framework learning form, laravel5 framework form
First, let us modify the routing to add the publication of an article.
Copy code The code is as follows:
Route::get('articles/create', 'ArticlesController@create');
Then modify the controller
Copy code The code is as follows:
Public function create() {
return view('articles.create');
}
We return a view and create a new view. Of course we could create the form directly using HTML, but we have a more functional way. We use an open source library, illuminatehtml developed by Jeffrey Way. Install dependent libraries:
Copy code The code is as follows:
composer require illuminate/html
Laravel’s library needs to be registered in laravel before it can be used. In config/app.php, we can see the provider field provided by laravel, which describes the library functions of laravel. In Laravel Framewirk Service Providers... finally add our new HtmlProvider
Copy code The code is as follows:
'IlluminateHtmlHtmlServiceProvider',
We don’t want to use a long name like IlluminateHtmlFromFacade to introduce it, we need a short name. Find the aliases section in the current app.php and add the alias at the end.
Copy code The code is as follows:
'Form' => 'IlluminateHtmlFormFacade',
'Html' => 'IlluminateHtmlHtmlFacade',
OK, now let’s create the view, views/articles/create.blade.php
@extends('layout') @section('content') <h1>Write a New Article</h1> <hr/> {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open() !!} {!! Form::close() !!} @stop
Visit /articles/create and see the error, Why? Let’s test to see what went wrong. Make the following changes in the controller:
public function show($id) { dd('show'); $article = Article::findOrFail($id); return view('articles.show', compact('article')); }
Yes, you read that right, the dd() method is added to the show method. This method simply outputs a message and then dies. Let’s visit /articles/create again. What do you see? You see the output of show .
Why did the route show us when we accessed create? Let’s take a look at the route and see what happened.
Copy code The code is as follows:
Route::get('articles', 'ArticlesController@index');
Route::get('articles/{id}', 'ArticlesController@show');
Route::get('articles/create', 'ArticlesController@create');
The above is our route, notice that articles/{id} means that this is a wildcard, everything after articles/ will match, do you know? Our /articles/create is also matched by him. OMG!
The solution is to adjust the order:
Copy code The code is as follows:
Route::get('articles', 'ArticlesController@index');
Route::get('articles/create', 'ArticlesController@create');
Route::get('articles/{id}', 'ArticlesController@show');
That is, from special to ordinary, you should always pay attention to this issue in future routing settings. Now we are accessing articles/create and everything is fine.
Check the source code in the browser, you will find that not only method and action are generated, but also a hidden _token field is generated as the server verifies the form to avoid forgery attacks by hackers.
Let’s modify our view and add fields:
@extends('layout') @section('content') <h1>Write a New Article</h1> <hr/> {{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open() !!} <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::submit('Add Article', ['class' => 'btn btn-primary form-control']) !!} </div> {!! Form::close() !!} @stop
When the form is submitted, it is actually submitted to articles/create using the post method. However, according to RESTful habits, we hope to be able to post to /articles. Let's modify the form method of the view and set the submission path.
Copy code The code is as follows:
{!! Form::open(['url' => 'articles']) !!}
Then we handle the form submission event in the route.
Copy code The code is as follows:
Route::post('/articles', 'ArticlesController@store');
Let’s handle the controller
//注意:将下面的 use 语句删除,我们使用 facade 接口中的 Request //use App\Http\Requests\Request; //引入下面的命名空间中的 Request use Illuminate\Support\Facades\Request; public function store() { //使用 Illuminate\Html\Request 来返回全部的表单输入字段 $input = Request::all(); //我们直接返回$input,来看一下 return $input; }
We can directly see the json result of the input form. If you only need the value of the title field, you can use Request::get('titel') .
How to add it to the database? With the help of the model, we can directly adopt the following method,
Article::create($input);
It’s that simple, it’s that willful
If you haven’t forgotten Mass Assignment, we defined the $fillable array in our model to define those fields that can be filled directly during create.
Modify the controller, add it to the model, and store it in the database.
public function store() { $input = Request::all(); Article::create($input); return redirect('articles'); }
Try adding a record, it’s great. But don't forget. We also have a field called published_at , let's deal with that.
public function store() { $input = Request::all(); $input['published_at'] = Carbon::now(); Article::create($input); return redirect('articles'); }
Add a new record and test it.
There is another problem. The newly added one should be displayed at the front. Let’s modify the following controller.
public function index() { //倒序获取文章 //可以这样 //$articles = Article::orderBy('published_at', 'desc')->get(); //简单方式,当然还有 oldest() $articles = Article::latest('published_at')->get(); return view('articles.index', compact('articles')); }
The above is the entire content of this article. I hope it will be helpful to everyone learning the Laravel5 framework.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Since Windows has become the gaming platform of choice, it's even more important to identify its gaming-oriented features. One of them is the ability to calibrate an Xbox One controller on Windows 11. With built-in manual calibration, you can get rid of drift, random movement, or performance issues and effectively align the X, Y, and Z axes. If the available options don't work, you can always use a third-party Xbox One controller calibration tool. Let’s find out! How do I calibrate my Xbox controller on Windows 11? Before proceeding, make sure you connect your controller to your computer and update your Xbox One controller's drivers. While you're at it, also install any available firmware updates. 1. Use Wind

How to implement page jump after PHP form submission [Introduction] In web development, form submission is a common functional requirement. After the user fills out the form and clicks the submit button, the form data usually needs to be sent to the server for processing, and the user is redirected to another page after processing. This article will introduce how to use PHP to implement page jump after form submission. [Step 1: HTML Form] First, we need to write a page containing a form in an HTML page so that users can fill in the data that needs to be submitted.

How to use JavaScript to realize the automatic prompt function of the input box content of the form? Introduction: The automatic prompt function of the form input box content is very common in web applications. It can help users quickly enter the correct content. This article will introduce how to use JavaScript to achieve this function and provide specific code examples. Create the HTML structure First, we need to create an HTML structure that contains the input box and the auto-suggestion list. You can use the following code: <!DOCTYP

PHP is a very popular programming language, and CodeIgniter4 is a commonly used PHP framework. When developing web applications, using frameworks is very helpful. It can speed up the development process, improve code quality, and reduce maintenance costs. This article will introduce how to use the CodeIgniter4 framework. Installing the CodeIgniter4 framework The CodeIgniter4 framework can be downloaded from the official website (https://codeigniter.com/). Down

Learning Laravel from scratch: Detailed explanation of controller method invocation In the development of Laravel, controller is a very important concept. The controller serves as a bridge between the model and the view, responsible for processing requests from routes and returning corresponding data to the view for display. Methods in controllers can be called by routes. This article will introduce in detail how to write and call methods in controllers, and will provide specific code examples. First, we need to create a controller. You can use the Artisan command line tool to create

How to use JavaScript to implement real-time verification of the input box content of a form? In many web applications, forms are the most common way of interaction between users and the system. However, the content entered by the user often needs to be validated to ensure the accuracy and completeness of the data. In this article, we will learn how to use JavaScript to implement real-time verification of the content of the form's input box and provide specific code examples. Creating the form First we need to create a simple table in HTML

How to handle user rights management in PHP forms With the continuous development of web applications, user rights management is one of the important functions. User rights management can control users' operating rights in applications and ensure the security and legality of data. In PHP forms, user rights management can be implemented through some simple code. This article will introduce how to handle user rights management in PHP forms and give corresponding code examples. 1. Definition and management of user roles First of all, defining and managing user roles is a matter of user rights.

How to use HTML, CSS and jQuery to implement the advanced function of automatic saving of forms. Forms are one of the most common elements in modern web applications. When users enter form data, how to implement the automatic saving function can not only improve the user experience, but also ensure data security. This article will introduce how to use HTML, CSS and jQuery to implement the automatic saving function of the form, and attach specific code examples. 1. Structure of HTML form. Let’s first create a simple HTML form.
