php 将数据从表单传递到控制器 Laravel 5

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/28927220/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:08:29  来源:igfitidea点击:

Passing data from form to controller Laravel 5

phplaravellaravel-5

提问by Shayle

I'm trying register users in a laravel 5 application using the restful controller.

我正在尝试使用 restful 控制器在 laravel 5 应用程序中注册用户。

The problem is that when I dump the data in my store function, I only get the csrf token, but not the values.

问题是,当我在存储函数中转储数据时,我只得到 csrf 令牌,而不是值。

Here's what I tried so far:

这是我到目前为止尝试过的:

  • Input::all();
  • Request::get();
  • Input::all();
  • Request::get();

Here's the code I'm executing:

这是我正在执行的代码:

Form

形式

<form class="form-horizontal" method="post" action="/users">

<div class="form-group">
    <label for="name" class="col-lg-2 control-label">
        Name
    </label>
    <div class="col-lg-10">
        <input type="text" class="form-control" id="name" value="a">
    </div>
</div>
<div class="form-group">
    <label for="email" class="col-lg-2 control-label">
        Email
    </label>
    <div class="col-lg-10">
        <input type="email" class="form-control" id="email" value="[email protected]">
    </div>
</div>
<div class="form-group">
    <label for="password" class="col-lg-2 control-label">
        Pw
    </label>
    <div class="col-lg-10">
        <input type="password" class="form-control" id="password">
    </div>
</div>
<div class="form-group">
    <div class="col-lg-10 col-lg-offset-2">
        <button type="reset" class="btn btn-default">Cancel</button>
        <button type="submit" class="btn btn-primary">Create</button>

    </div>
</div>
<input type="hidden" name="_token" value="{{ csrf_token() }}">
</form>

Controller

控制器

public function store()
{
    $input = Input::only('name','email','password');            
    $user = new User;
    $user->name = $input['name'];
    $user->email = $input['email'];
    $user->password = Hash::make($input['password']);
    $user->save();
}

And Routeis just

Route只是

Route::resource('users','UsersController');

回答by lukasgeiter

Your input fields are all missing a nameattribute! For example

您的输入字段都缺少一个name属性!例如

<input type="text" class="form-control" id="name" name="name" value="a">
<!--                                              ^^^^^^^^^^^        -->

回答by Asad Ali Jan

You should put nameattributes on the input fields, because html forms communicate with php with nameattribute.

您应该将name属性放在输入字段上,因为 html 表单通过name属性与 php 通信。

Example:

例子:

<input........... name="etc">

and in controller use the nameyou have given an input as a key :

并在控制器中使用name您提供的输入作为键:

 $user->name = $input['etc'];

 <input type="text" class="form-control" id="name" name="name" value="a">