This works on create/store but not on edit/update. I want the user to be updated and the data already exists in the database, the validation error appears. like this in the user store, I added the data, and it worked, even if there was already the same data then a validation error appeared, but in a different update if I update only the address then the old username data is still used and if I change the username it also works but it doesn't if I replace the username with an existing username the validation error does not appear and instead displays the following error. please help me i am still a student!
Error
SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'asdf' for key 'user_username_unique' (SQL: update `user` set `username` = asdf, `password` = $2y$10$BYdDToN5jCCuRLdZx70YA.BFgyVIWulL8n/bv5C3VxOVCw6WBN.kO, `kota_id` = 1505, `kecamatan_id` = 1505013, `desa_id` = 1505013004, `user`.`updated_at` = 2022-04-09 14:25:03 where `id` = 4)
User Migration
Schema::create('user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nik')->unique()->nullable();
$table->string('nama')->nullable();
$table->string('telp')->unique()->nullable();
$table->string('email')->unique();
$table->timestamp('email_verified_at')->nullable();
$table->string('foto')->nullable();
$table->string('username')->unique();
$table->string('password');
$table->enum('level', ['user','admin'])->default('user');
$table->unsignedBigInteger('provinsi_id')->nullable();
$table->unsignedBigInteger('kota_id')->nullable();
$table->unsignedBigInteger('kecamatan_id')->nullable();
$table->unsignedBigInteger('desa_id')->nullable();
$table->text('alamat')->nullable();
$table->rememberToken();
$table->timestamps();
});
User Controller Store
public function store(Request $request)
{
$validasi = $request->validate([
'username' => ['required', 'string', 'min:3', 'max:30', 'unique:user'],
'email' => ['required', 'email', 'string', 'max:255', 'unique:user'],
'password' => ['required', 'string', 'min:8'],
'nama' => ['required', 'string', 'min:3', 'max:50'],
'nik' => ['required', 'string', 'min:16', 'max:16', 'unique:user'],
'telp' => ['required', 'string', 'min:12', 'max:13', 'unique:user'],
'provinsi_id' => ['required'],
'kota_id' => ['required'],
'kecamatan_id' => ['required'],
'desa_id' => ['required'],
'foto' => ['mimes:jpeg,jpg,png'],
'level' => ['required'],
'alamat' => ['required'],
]);
$validasi['password'] = Hash::make('password');
$create = User::create($validasi);
if($request->hasFile('foto')){
$request->file('foto')->move('images/',$request->file('foto')->getClientOriginalName());
$create->foto = $request->file('foto')->getClientOriginalName();
$create->save();
}
return redirect()->route('user.index');
}
User Controller Update
public function update(Request $request, $id)
{
$user = User::find($id);
$validasi = $request->validate([
'username' => ['required', 'string', 'min:3', 'max:30', 'unique:user,id'],
'email' => ['required', 'email', 'string', 'max:255', 'unique:user,id'],
'password' => ['required', 'string', 'min:8', 'max:20'],
'nama' => ['required', 'string', 'min:3', 'max:50'],
'nik' => ['required', 'string', 'min:16', 'max:16', 'unique:user,id'],
'telp' => ['required', 'string', 'min:12', 'max:13', 'unique:user,id'],
'provinsi_id' => ['required'],
'kota_id' => ['required'],
'kecamatan_id' => ['required'],
'desa_id' => ['required'],
'foto' => ['mimes:jpeg,jpg,png'],
'level' => ['required'],
'alamat' => ['required'],
]);
$validasi['password'] = Hash::make('password');
$user->update($validasi);
if($request->hasFile('foto')){
$request->file('foto')->move('images/',$request->file('foto')->getClientOriginalName());
$user->foto = $request->file('foto')->getClientOriginalName();
$user->save();
}
return redirect()->route('user.index');
}
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire