gpt4 book ai didi

php - Laravel Form PUT 方法不起作用

转载 作者:塔克拉玛干 更新时间:2023-11-03 05:58:53 27 4
gpt4 key购买 nike

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

表格:

{!! 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;
}
}

Controller :

/**
* 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'
];
}

如果可能请帮忙。谢谢!

最佳答案

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

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

@include('roles.fields')

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

关于php - Laravel Form PUT 方法不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39125029/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com