how do i store name value instead of id when i use select option like this :
<select type="text" name="jenis_hewan_id" id="jenis_hewan_id" class="form-control">
<option value="">Choose Animal</option>
@foreach ($jenisHewan as $data)
<option value="{{$data->id}}">{{$data->nama_jenis_hewan }}</option>
@endforeach
</select>
i got error when i store that, like this :
SQLSTATE[22007]: Invalid datetime format: 1366 Incorrect integer value: 'Anjing' for column hewan_siapa.ras_hewans.jenis_hewan_id at row 1 (SQL: insert into ras_hewans (nama_ras_hewan, jenis_hewan_id) values (Sphynx, Anjing))
but when i insert with <option value="{{$data->id}}" , it's work but it store as 'id' value ,not 'name' value in my database, how do i fix this?
My Controller:
public function index()
{
$this->data['jenisHewan'] = JenisHewan::pluck('nama_jenis_hewan');
$this->data['RasHewan'] = RasHewan::paginate(5);
// $this->data['getParentOption'] = JenisHewan::where('id')->pluck('nama_jenis_hewan');
$this->data['sortDataByName'] = RasHewan::latest('nama_ras_hewan');
return view('admin.rashewan.ras-hewan-index',$this->data);
}
public function store(Request $request)
{
$namaJenisHewan = (String)$request->jenis_hewan_id;
$validator = $request->validate([
'nama_ras_hewan' => 'required|string|min:3',
'jenis_hewan_id' => 'required|string|',
'parent_ras_jenis_hewan'=> 'string',
], [
'nama_ras_hewan.required' => 'Ras Hewan tidak boleh kosong',
]
);
$rasHewans = new RasHewan();
$rasHewans->nama_ras_hewan = $request->nama_ras_hewan;
$rasHewans->jenis_hewan_id = $namaJenisHewan;
$rasHewans->parent_ras_jenis_hewan = $request->parent_ras_jenis_hewan;
// $rasHewan = New RasHewan;
// $rasHewan->parent_ras_jenis_hewan = $request->$getParentOption;
// $rasHewan->save();
RasHewan::create($validator);
return dd($validator);
// if(validator()) {
// return redirect()->route('rashewan.index')
// ->with('success', 'Data '.$request->nama_ras_hewan .' telah selesai dibuat.');
// } else {
// return redirect()->route('rashewan.index')->with('error','Data gagal dibuat');
// }
}
The issue is with controller.First remove this line below
and then modify
RasHewanlike below