Let's first modify the route to add a post.
Copy the Code code as follows:
Route::get (' articles/create ', ' articlescontroller@create ');
Then modify the controller
Copy the Code code as follows:
Public Function Create () {
Return view (' Articles.create ');
}
We return to a view and create this view. We can of course use HTML to create a form, but we have a better way to do it. We use an open source library, the Jeffrey-D illuminate\html. To install a dependent library:
Copy the Code code as follows:
Composer require illuminate/html
Laravel libraries need to be registered with Laravel in order to use them. 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
Copy the Code code as follows:
' Illuminate\html\htmlserviceprovider ',
We don't want to use the name Illuminate\html\fromfacade so long to introduce, we need a short name. Locate the aliases segment in the current app.php and add the alias at the end.
Copy the Code code 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
{{--using the illuminate\html Open Source Library We added--}} {!! Form::open ()!!} {!! Form::close ()!!} @stop
Visit/articles/create saw an error, why? Let's test where the problem is. Make the following modifications in the controller:
Public function Show ($id) { dd (' show '); $article = Article::findorfail ($id); Return view (' Articles.show ', compact (' article ')); }
Yes, you're right, just add the DD () method to the Show method, and this method simply outputs a message and then dies. We'll visit/articles/create again and you see what you see out of the show.
Why did we visit the create results route to our show? Let's take a look at the route and what happened.
Copy the Code code 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, all the things behind articles/will match, you know? Our/articles/create was also matched by him. Omg!
The solution is to adjust the order:
Copy the Code code as follows:
Route::get (' articles ', ' Articlescontroller@index ');
Route::get (' articles/create ', ' articlescontroller@create ');
Route::get (' Articles/{id} ', ' articlescontroller@show ');
That is, from the special to the normal, the future of the routing settings should always pay attention to this problem. Now we are visiting articles/create everything OK.
Looking at the source code in the browser, you will find that not only the method and the action generated a hidden _token field as the server to verify the form, to avoid the hacker's forgery attack.
Let's modify our view to add a field:
@extends (' layout ') @section (' content ') Write a New article
{{--using 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 restful habits, we want to be able to post to/articles, we can modify the View form method, set the path of the submission.
Copy the Code code as follows:
{!! Form::open ([' url ' = ' articles ')]!
We then process the form submission events in the route.
Copy the Code code as follows:
Route::p ost ('/articles ', ' articlescontroller@store ');
Let's handle the controller.
Note: To remove the following use statement, we used the Request//use app\http\requests\request;//in the facade interface to introduce Requestuse Illuminate\support in the following namespace \facades\request; Public function Store () { //Use Illuminate\html\request to return all form input fields $input = Request::all (); We return directly to the $input, and take a look at return $input; }
We can see the JSON results of the input form directly. If you only need the value of the Title field, you can use Request::get (' Titel ').
How do I add it to the database? Using the model, we can use the following method directly,
Article::create ($input);
It's so simple, it's so willful
If you have not forgotten Mass assignment, in our model we have defined $fillable arrays to define those fields that can be directly populated directly at 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 '); }
Add a record try it, it's great. But don't forget. We also have a field called Published_at, 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 also a problem that the newly added should be shown in front of us to modify the following controllers.
Public Function Index () { //Reverse Get Article //can be this //$articles = Article::orderby (' published_at ', ' desc ')->get (); The simple way, of course, is oldest () $articles = article::latest (' published_at ')->get (); Return view (' Articles.index ', compact (' articles ')); }
The above mentioned is the whole content of this article, I hope to be able to learn LARAVEL5 framework to help you.