Laravel Five Basics (ix)-form

Source: Internet
Author: User
Tags compact
Laravel 5 Basics (ix)-form

Let's first modify the route to add a post.

Route::get('articles/create', '[email protected]');

Then modify the controller

    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:

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 fields provided by Laravel provider , which describes the library functions of Laravel. In the Laravel Framewirk Service Providers... end, add our newHtmlProvider

'Illuminate\Html\HtmlServiceProvider',

We don't want Illuminate\Html\FromFacade to use such a long name to introduce, we need a short name. The aliases segment is found in the current app.php , and the alias is added at the end.

'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

{{--使用我们添加的 illuminate\html 开源库--}} {!! Form::open() !!} {!! Form::close() !!}@stop

The visit /articles/create saw an error, why? Let's test what's wrong. 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 are right, just show add a method to the method dd() , this method simply outputs a message and then dies. We'll visit again /articles/create and you see what you see in the output show .

Why did we access the create results routed to us show ? Let's look at the route and what happened.

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

The above is our route, notice that articles/{id} this is a wildcard, all the things in the articles/ back will match, you know? We /articles/create were also matched by him. Omg!

The solution is to adjust the order:

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

That is, from the special to the normal, the future of the routing settings should always pay attention to this problem. Now we are in the access to articles/create everything OK.

Looking at the source code in the browser, you will find that not only generated method and 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

{{--使用我们添加的 illuminate\html 开源库--}} {!! 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 used to post submit the method to articles/create the top, but according to the restful habits, we want to be able post to modify the /articles view's form method, set the path of the submission.

{!! Form::open(['url' => 'articles']) !!}

We then process the form submission events in the route.

Route::post('/articles', '[email protected]');

Let's handle the controller.

//注意:将下面的 use 语句删除,我们使用 facade 接口中的 Request//use App\Http\Requests\Request;//引入下面的命名空间中的 Requestuse Illuminate\Support\Facades\Request;    public function store() {        //使用 Illuminate\Html\Request 来返回全部的表单输入字段        $input = Request::all();        //我们直接返回$input,来看一下        return $input;    }

We can see the JSON results of the input form directly. If you want only title the value of the field, you can use it 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 do not forget Mass assignment, in our model we define $fillable arrays to define which fields can be directly populated directly at create the time.

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() {        //倒序获取文章        //可以这样        //$articles = Article::orderBy('published_at', 'desc')->get();        //简单方式,当然还有 oldest()        $articles = Article::latest('published_at')->get();        return view('articles.index', compact('articles'));    }
  • 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.