我有一个通过TruckController
生成的资源控制器php artisan make:controller TruckController --resource
。
当我转到localhost/trucks/1/edit
时,编辑表单会成功显示。
当我单击提交时,我将重定向到资源显示页面(localhost/trucks/1
);但是,卡车信息不会更新,也不会引发任何错误。
似乎update
函数根本没有触发,我认为这意味着我的表单中的action
属性存在问题,但我不确定。路由应该没有问题,因为路由是资源控制器,因此应该已经定义了路由。
edit.blade.php
<form method="PUT" action="{{ action('TruckController@update', ['id' => $truck->id]) }}" class="validate form-horizontal form-groups-bordered" novalidate="novalidate">
@csrf
<input type="hidden" name="state" data-geo="administrative_area_level_1" value="{{ $truck->state }}">
<input type="hidden" name="city" data-geo="locality" value="{{ $truck->city }}">
<input type="hidden" name="zipcode" data-geo="postal_code" value="{{ $truck->zipcode }}">
[ ... ]
</form>
TruckController @ update
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(TruckCreateRequest $request, Truck $truck)
{
$validated = $request->validated();
$truck->name = $request->name;
$truck->description = $request->description;
$truck->save();
}
TruckCreateRequest.php
<?php
namespace App\Http\Requests;
use Illuminate\Foundation\Http\FormRequest;
class TruckCreateRequest extends FormRequest
{
/**
* 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 [
'name' => 'required',
'description' => 'required',
'city' => 'required',
'state' => 'required',
'zipcode' => 'required',
'address' => 'required'
];
}
}
以防万一这可能会有更多帮助...
Web.php
<?php
Auth::routes();
// ajax
Route::post('ajax/notifications/clear', 'DashboardController@mark_notifications_read');
Route::get('/', 'DashboardController@index')->name('dash');
Route::resource('events', 'EventController');
Route::resource('venues', 'VenueController');
Route::resource('trucks', 'TruckController');
Route::resource('admin', 'AdminController');
App / Models / Truck.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Truck extends Model
{
//
}
答案 0 :(得分:2)
从Laravel文档中:
由于HTML表单无法发出PUT,PATCH或DELETE请求,因此您将 需要添加一个隐藏的_method字段来欺骗这些HTTP动词。
尝试向表单添加隐藏的方法字段:
@method('PUT')
答案 1 :(得分:1)
您应该尝试以下操作:
<form method="POST" action="{{ action('TruckController@update', [$truck->id]) }}" class="validate form-horizontal form-groups-bordered" novalidate="novalidate">
@csrf
<input type="hidden" name="state" data-geo="administrative_area_level_1" value="{{ $truck->state }}">
<input type="hidden" name="city" data-geo="locality" value="{{ $truck->city }}">
<input type="hidden" name="zipcode" data-geo="postal_code" value="{{ $truck->zipcode }}">
[ ... ]
</form>