I want to update datas in database in Laravel, but I get error. Here is my Route:
Route::post('/resetPOST', [App\Http\Controllers\Update::class, 'Update'])->name('id');
Here is my page:
<form method="POST" action="/resetPOST">
@csrf
<input type="hidden" name="id" value="{{ Auth::user()->id }}">
<input type="text" name="name" required value="{{ Auth::user()->name }}">
<input type="text" name="surname" required value="{{ Auth::user()->surname }}">
<input type="text" name="middlename" required value="{{ Auth::user()->middlename }}">
<input type="email" name="email" autocomplete="off" required value="{{ Auth::user()->email }}">
<input type="date" name="dateofbirth" required value="{{ Auth::user()->dateofbirth }}">
<input type="text" name="idnumber" required value="{{ Auth::user()->idnumber }}">
<button type="submit" >Изменить</button>
</form>
Here is my App\Http\Controllers\Update code:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class Update extends Controller
{
function update($id, $name, $surname, $middlename, $email, $datebirth, $idnumber){
echo $id;
// DB::table('users')-->where('id', '=', $id ->update(['name' => $name,'surname' => $surname,'middlename' => $middlename,'email' => $email,'datebirth' => $datebirth, 'idnumber' => $idnumber]));
}
}
This is my error:
ArgumentCountError
Too few arguments to function App\Http\Controllers\Update::update(), 0 passed in D:\openserver\OpenServer\domains\localhost\laravel\right-univercity\vendor\laravel\framework\src\Illuminate\Routing\Controller.php on line 54 and exactly 7 expected
CodePudding user response:
You definitely should not have id
in your form. A user would be able to manipulate that and update anyones profile. id
should just be taken from Auth::user()->id
(within your update method) without ever appearing in your form.
Try changing to this:
<?php
namespace App\Http\Controllers;
use App\Models\User;
use Illuminate\Http\Request;
class Update extends Controller
{
public function update(Request $request)
{
$name = $request->input('name');
// do the same for everything else..
// echo $name;
DB::table('users')
->where('id', '=', Auth::user()->id)
->update([
'name' => $name
]);
redirect()->back();
}
}