【问题标题】:Passing data from form to controller Laravel 5将数据从表单传递到控制器 Laravel 5
【发布时间】:2015-03-08 13:55:08
【问题描述】:

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

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

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

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

这是我正在执行的代码:

表格

<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="a@a.Fr">
    </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>

控制器

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();
}

路线只是

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

【问题讨论】:

    标签: php laravel laravel-5


    【解决方案1】:

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

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

    【讨论】:

    • 已经编码 10 年了,我也忘记了哈哈哈 ;)
    • 刚来到这个帖子,遇到了完全相同的问题-_-
    • 哎呀...谢谢!
    【解决方案2】:

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

    例子:

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

    并在控制器中使用name,您已将输入作为键:

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

     &lt;input type="text" class="form-control" id="name" name="name" value="a"&gt;

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-11
      • 1970-01-01
      • 2015-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-12-27
      • 2019-01-06
      相关资源
      最近更新 更多