Im trying to submit entries to database with automatic expiration date calculated 1 week after the creation date. The problem is, i don't know how to add the variable i made in the store controller to be created together with the inputs on the create form.
This is my create form :
<div >
<!-- Page Heading -->
<h1 >Pasien Baru</h1>
<p >Konfigurasi untuk mendaftarkan pasien</p>
<div>
<form method="post" action="/dashboard/pasien" enctype="multipart/form-data">
@csrf
<div >
<label for="nik">NIK Pasien</label>
<input maxlength="16" type="text" id="nik" name="nik" placeholder="Nomor Induk Kependudukan">
@error ('nik')
<div >
{{ $message }}
</div>
@enderror
</div>
<div >
<label for="name">Nama Pasien</label>
<input type="text" id="name" name="name" placeholder="Nama Lengkap">
@error ('name')
<div >
{{ $message }}
</div>
@enderror
</div>
<div >
<label for="lahir">Tanggal Lahir</label>
<input type="date" id="lahir" name="lahir">
@error ('lahir')
<div >
{{ $message }}
</div>
@enderror
</div>
<div >
<label for="kelamin">Jenis Kelamin</label><br>
<select name="kelamin" style="width: 200px">
<option selected value="pasien">Laki-laki</option>
<option value="admin">Perempuan</option>
</select>
@error ('kelamin')
<div >
{{ $message }}
</div>
@enderror
</div>
<div >
<label for="poli">Layanan</label><br>
<select name="poli" style="width: 200px">
@foreach ($layanan as $poli)
<option value="{{ $poli->singkatan }}">{{ $poli->jenis }}</option>
@endforeach
</select>
@error ('poli')
<div >
{{ $message }}
</div>
@enderror
</div>
<div >
<label for="bayar">Jenis Pembayaran</label><br>
<select name="bayar" style="width: 200px">
<option selected value="umum">Umum</option>
<option value="bpjs">BPJS Kesehatan</option>
<option value="kis">JKN-KIS</option>
</select>
@error ('bayar')
<div >
{{ $message }}
</div>
@enderror
</div>
<div >
</div>
<button type="submit" >Daftarkan</button>
</form>
<a href="/dashboard/pasien/" >Batal</a>
</div>
</div>
This is my laravel store controller. The $expiry_day is working fine, but i dont know how to add it together in the create method. I want so that the $expiry_day become the 'expired' value :
public function store(Request $request)
{
$start_day = Carbon::parse($request->created_at);
$expiry_day = $start_day->addWeek();
$validateData = $request->validate([
'nik' => 'required',
'name' => 'required',
'kelamin' => 'required',
'lahir' => 'required',
'bayar' => 'required',
'expired' => 'required',
'poli' => 'required',
]);
pasien::create($validateData);
return redirect('/dashboard/pasien')->with('success','Data pasien berhasil ditambahkan');
}
CodePudding user response:
You can simply add date to the validated data array. This will then be inserted when you create the model.
$validatedData = $request->validate([...]);
$start_day = Carbon::parse($request->created_at);
$validatedData['expired'] = $start_day->addWeek();
pasien::create($validateData);
This requires that expired
is a column in the database and is in the fillable array on the Pasien
model.
Bonus: please don't call classes in lowercase, it is universal agreed upon that classes starts with a capital letter.
Pasien::create($validateData);