In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly introduces "how to write the form of Laravel 5 framework". In the daily operation, I believe that many people have doubts about how to write the form of Laravel 5 framework. The editor has consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful for you to answer the doubt of "how to write the form of Laravel 5 framework". Next, please follow the editor to study!
The copy code is as follows:
Route::get ('articles/create',' ArticlesController@create')
Then modify the controller
The copy code is as follows:
Public function create () {
Return view ('articles.create')
}
We return to a view and create a new view. Of course we can build forms directly using HTML, but we have a better way to do it. We use an open source library, illuminate\ html developed by Jeffrey Way. Install dependent libraries:
The copy code is as follows:
Composer require illuminate/html
Laravel's library needs to be registered with laravel before it can be used. In config/app.php, we can see the provider field provided by laravel, which describes the library functionality of laravel. In Laravel Framewirk Service Providers... Finally, add our new HtmlProvider.
The copy code is as follows:
'Illuminate\ Html\ HtmlServiceProvider'
We don't want to introduce it with a name as long as Illuminate\ Html\ FromFacade, we need a short name. Find the aliases segment in the current app.php and add an alias at the end.
The copy code is as follows:
'Form' = > 'Illuminate\ Html\ FormFacade'
'Html' = > 'Illuminate\ Html\ HtmlFacade'
OK, now let's create the view, views/articles/create.blade.php
@ extends ('layout') @ section (' content') Write a New Article {{- use the illuminate\ html open source library we added--} {!! Form::open ()! {! Form::close ()!} @ stop
Access / articles/create saw an error, Why? Let's test 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 it correctly, just add the dd () method to the show method, which simply outputs a message and dies. Let's visit / articles/create again, what do you see, you see the output show.
Why did we visit create and get routed to us show? Let's take a look at the routing and what's going on.
The copy code is as follows:
Route::get ('articles',' ArticlesController@index')
Route::get ('articles/ {id}', 'ArticlesController@show')
Route::get ('articles/create',' ArticlesController@create')
Here is our route, notice that articles/ {id} means that this is a wildcard, and everything after articles/ will match, do you know? He also matched our / articles/create. OMG!
The solution is to adjust the order:
The copy 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, we should always pay attention to this problem in the future routing settings. Now we are visiting all the OK of articles/create.
If you look at 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 a server to validate the form to avoid forgery attacks by hackers.
Let's modify our view and add fields:
@ extends ('layout') @ section (' content') Write a New Article {{- use the illuminate\ html open source library we added--} {!! Form::open ()! {! Form::label ('title',' Title:')!} {!! Form::text ('title', null, [' class' = > 'form-control'])!} {! Form::label ('body',' Body:')!} {!! Form::textarea ('body', null, [' class' = > 'form-control'])!} {! Form::submit ('Add Article', [' class' = > 'btn btn-primary form-control'])!} {!! Form::close ()!} @ stop
When the form is submitted, it is actually submitted to articles/create using the post method, but according to the custom of RESTful, we want to be able to post to / articles. Let's modify the form method of the view and set the submission path.
The copy code is as follows:
{!! Form::open (['url' = >' articles'])!}
Then we handle the form submission event in the route.
The copy code is as follows:
Route::post ('/ articles', 'ArticlesController@store')
Let's deal with the controller.
/ / Note: by deleting the following use statement, we use Request//use App\ Http\ Requests\ Request;// in the facade interface to introduce Requestuse Illuminate\ Support\ Facades\ Request; public function store () in the following namespace {/ / use Illuminate\ Html\ Request to return all form input fields $input = Request::all (); / / Let's directly return $input to take a look at return $input;}
We can see the json result of the input form directly. If you only need the value of the title field, you can use Request::get ('titel').
How do you add it to the database? With the help of the model, we can directly use the following methods
Article::create ($input)
It's that simple, it's so capricious.
If you don't forget Mass Assignment, we define the $fillable array in our model to define which fields can be populated directly in 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 to add a record. It's great. But don't forget. We also have a field called published_at, so let's deal with it.
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 should be displayed at the front, let's modify the following controller.
Public function index () {/ / get the article in reverse order / / you can get the article like this / / $articles = Article::orderBy ('published_at',' desc')-> get (); / / simple way, of course, there is oldest () $articles = Article::latest ('published_at')-> get (); return view (' articles.index', compact ('articles')) At this point, the study on "how to write a form for a Laravel 5 framework" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.