【问题标题】:Laravel Form Model Binding not submittingLaravel 表单模型绑定未提交
【发布时间】:2014-12-02 13:58:43
【问题描述】:

我在使用带有 L4 的表单模型绑定时遇到了一些问题。正在填充我的表单,路线正确,但未正确提交。

控制器:

public function edit($id)
{
   $transaction = Transaction::where('id', '=', $id)->get();
    return View::make('transaction')->with('transactions', $transaction);

}

public function update($id)
{
    $transaction = Transaction::find($id);
    $input = Input::all();
    $transaction->status = $input['status'];
    $transaction->description = $input['description'];
    $transaction->save();
}

查看:

@foreach($transactions as $transaction)
{{ Form::model($transaction, array('route' => array('transactions.update', $transaction->id))); }}
{{ Form::text('description'); }}
{{ Form::select('status', array('R' => 'Recieved', 'S' => 'Shipped', 'P' => 'Pending'), 'R'); }}
{{ Form::submit('Submit'); }}
{{ Form::close(); }} 
@endforeach

【问题讨论】:

  • transactions.*的路由是由Route::resource()生成的吗?
  • 你是如何声明路由的?在此处发布。

标签: php forms laravel model


【解决方案1】:

我假设您的 transactions.* 路由是通过 Route::resource() 生成的。

Per the documentation,Laravel 为资源生成以下路由:

Verb      Path                        Action  Route Name
GET       /resource                   index   resource.index
GET       /resource/create            create  resource.create
POST      /resource                   store   resource.store
GET       /resource/{resource}        show    resource.show
GET       /resource/{resource}/edit   edit    resource.edit
PUT/PATCH /resource/{resource}        update  resource.update
DELETE    /resource/{resource}        destroy resource.destroy

您会看到resource.update 期待PUT/PATCH 请求,但Laravel forms default to POST

要解决此问题,请将'method' => 'PUT' 添加到表单选项数组中,如下所示:

{{ Form::model($transaction, array(
    'method' => 'PUT',
    'route'  => array('transactions.update', $transaction->id)
)); }}

这会在表单中添加一个隐藏输入 <input type="hidden" name="_method" value="PUT" />,告诉 Laravel 将请求伪装成 PUT

【讨论】:

    猜你喜欢
    • 2013-05-28
    • 1970-01-01
    • 2014-08-07
    • 2018-04-09
    • 2018-05-10
    • 1970-01-01
    • 1970-01-01
    • 2013-11-02
    • 1970-01-01
    相关资源
    最近更新 更多