Home > OS >  Can't save data to database from input form - Laravel 8
Can't save data to database from input form - Laravel 8

Time:05-15

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:

https://codeshare.io/3AzKD1

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.

CodePudding user response:

This is solved.

I need to fill all the field on the table so it can store to the database.

  • Related