'Can't save data to database from input form - Laravel 8
it's me again. I have a problem saving data to the database from the input form that I made.
Here is my web route:
Route::get('/admin/data-karyawan', [BasicKaryawanController::class, 'data_karyawan']);
Route::get('/admin/create', [BasicKaryawanController::class, 'index']);
Route::post('/admin/create', [BasicKaryawanController::class, 'simpan']);
Here is my controller:
class BasicKaryawanController extends Controller
{
public function data_karyawan()
{
return view('data-karyawan', [
"title" => "Data Karyawan",
"karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
]);
}
public function index()
{
return view('create', [
"title" => "Tambah Karyawan"
]);
}
public function simpan(Request $request)
{
$validatedData = $request->validate([
'nik' => 'required|max:7',
'nama' => 'required|max:255',
'jk' => 'required|max:1',
'tempat_lahir' => 'required|max:255',
'tanggal_lahir' => 'required|max:255',
'alamat' => 'required|max:255',
'agama' => 'required',
'jabatan' => 'required|max:255',
'id_jabatan' => 'required|max:1',
'divisi' => 'required',
'email' => 'required|email:dns|unique:karyawans',
'password' => 'required|min:5|max:255'
]);
$validatedData['password'] = bcrypt($validatedData['password']);
Karyawan::create($validatedData);
return view('data-karyawan', [
"title" => "Data Karyawan",
"karyawan" => Karyawan::with(['role', 'user'])->search(request(['search']))->paginate(10)
]);
}
and this is my form blade view:
The code is running well but the form is not saving the data I input from the form to the database. Did I miss something?
Thank you.
Solution 1:[1]
This is solved.
I need to fill all the field on the table so it can store to the database.
Yes right, if u r using eloquent create method which accepts an array of attributes, creates a model, and inserts it into the database. The difference between save and create is that save accepts a full Eloquent model instance while create accepts a plain PHP array.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | VANITA CHAUDHARI |