Laravel Form PUT方法不起作用

时间:2016-08-24 13:43:47

标签: php forms laravel-5.2 put

我创建了一个表单来更新角色模型,但是在点击“保存”按钮时,它似乎没有提交给控制器方法。如果名称输入字段无效,则甚至不会出现验证错误消息。您可以在下面找到用于表单的代码。

形式:

{!! Form::model($role, ['route' => ['roles.update', $role->id], 'method' => 'put']) !!}

   @include('roles.fields')

{!! Form::close() !!}

表单的字段为:

<!-- Name Field -->
<div class="form-group col-sm-6">
   {!! Form::label('name', 'Name:') !!}
   {!! Form::text('name', null, ['class' => 'form-control']) !!}
</div>

<!-- Display Name Field -->
<div class="form-group col-sm-6">
   {!! Form::label('display_name', 'Display Name:') !!}
   {!! Form::text('display_name', null, ['class' => 'form-control']) !!}
</div>

<!-- Description Field -->
<div class="form-group col-sm-12 col-lg-12">
   {!! Form::label('description', 'Description:') !!}
   {!! Form::textarea('description', null, ['class' => 'form-control',   'rows' => '5']) !!}
</div>

<!-- Submit Field -->
<div class="form-group col-sm-12">
   {!! Form::submit('Save', ['class' => 'btn btn-primary']) !!}
   <a href="{!! route('roles.index') !!}" class="btn btn-  default">Cancel</a>
</div>

请求:

namespace App\Http\Requests;

use App\Http\Requests\Request;
use App\Models\Role;

class UpdateRoleRequest extends Request
{

    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
     public function authorize()
     {
         return true;
     }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
     public function rules()
     {
         return Role::$rules;
     }
}

控制器:

/**
 * Update the specified Role in storage.
 *
 * @param  int              $id
 * @param UpdateRoleRequest $request
 *
 * @return Response
 */
public function update($id, UpdateRoleRequest $request)
{
    $role = $this->roleRepository->findWithoutFail($id);

    if (empty($role)) {
        Flash::error('Role not found');

        return redirect(route('roles.index'));
    }

    $role = $this->roleRepository->update($request->all(), $id);

    Flash::success('Role updated successfully.');

    return redirect(route('roles.index'));
}

型号:

<?php

namespace App\Models;

use Eloquent as Model;
use Zizaco\Entrust\EntrustRole;
use Illuminate\Database\Eloquent\SoftDeletes;

class Role extends EntrustRole
{
    use SoftDeletes;

    public $table = 'roles';

    protected $dates = ['deleted_at'];

    public $fillable = [
        'name',
        'display_name',
        'description'
    ];

    /**
     * The attributes that should be casted to native types.
     *
     * @var array
     */
    protected $casts = [
        'name' => 'string',
        'display_name' => 'string'
    ];

    /**
     * Validation rules
     *
     * @var array
     */
    public static $rules = [
        'name' => 'required|unique:roles'
    ];
}

请尽可能帮助。谢谢!

2 个答案:

答案 0 :(得分:10)

您无法使用put作为表单方法。阅读关于laravel中方法欺骗的documentation

{!! Form::model($role, ['route' => ['roles.update', $role->id], 'method' => 'post']) !!}
   <input type="hidden" name="_method" value="PUT">

   @include('roles.fields')

{!! Form::close() !!}

答案 1 :(得分:2)

对于那些使用HTML和laravel 5.2的人:

<form method="post" ... > {{ method_field('PUT') }} ... </form>
希望这有助于人们。