Reputation: 21
So, I'm trying to send my new stats to the update function in my CharactersController, but it say this error: 'MethodNotAllowedHttpException'.I tried method spoofing, but that didn't work.
Here is my index where I want to edit the character($tet is a row function of my character that I'm watching):
{!! Form::open(['action' => ['CharactersController@update', $tet->id], 'method' => 'POST','id'=>'form1', 'enctype' => 'multipart/form-data']) !!}
<tr>
<td>HP:</td>
<td>{{$tet->hp}}</td>
<td><button class="btn btn-primary" onclick="addValue(1)">+</button></td>
<td><p>-></p></td>
<td><input id="jedna" class="edit" name="hodnota" type="number" disabled="true" value={{$tet->hp}}></td>
</tr>
<tr>
<td>ATTACK:</td>
<td>{{$tet->attack}}</td>
<td><button class="btn btn-primary" onclick="addValue(2)">+</button></td>
<td><p>-></p></td>
<td><input id="dva" class="edit" name="hodnota" type="number" disabled="true" value={{$tet->attack}}></td>
</tr>
<tr>
<td>LUCK:</td>
<td>{{$tet->luck}}</td>
<td><button class="btn btn-primary" onclick="addValue(3)">+</button></td>
<td><p>-></p></td>
<td><input id="tri" class="edit" name="hodnota" type="number" disabled="true" value={{$tet->luck}}></td>
</tr>
<tr>
<td>AGILITY:</td>
<td>{{$tet->agility}}</td>
<td><button class="btn btn-primary" onclick="addValue(4)">+</button></td>
<td><p>-></p></td>
<td><input id="ctyri" class="edit" name="hodnota" type="number" disabled="true" value={{$tet->agility}}></td>
</tr>
<button onclick="uloz()">SAVE ALL</button>
{{Form::hidden('_method','PUT')}}
{{Form::close()}}
In the 'uloz()' function is that I enable all number inputs and submit the form. This is my CharactersContrller:
public function update(Request $request, $id)
{
$character = Character::find($id);
$character->hp=$request->input('jedna');
$character->attack=$request->input('dva');
$character->luck=$request->input('tri');
$character->agility=$request->input('ctyri');
$character->save();
return redirect('/character');
}
Upvotes: 1
Views: 88
Reputation: 21
Ok so I got it, be careful on your HTML structure, I had my form inside the <table>
, but it has to be outside. That was all, no CSRF token needed.
Upvotes: 1
Reputation:
You should try this:
Your view file like this:
{!! Form::model($tet, ['route' => ['characters.update', $tet->id], 'class' => 'form-horizontal', 'role' => 'form', 'method' => 'post']) !!}
Your route like this:
Route::post('characters/update/{id}', 'CharactersController@update')->name('characters.update');
Updated Answer
Please add CSRF
token in your form like:
<meta name="csrf-token" content="{{ csrf_token() }}">
Add csrf token in ajax:
$.ajaxSetup({
headers: {
'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
}
});
Upvotes: 0