I'm trying to add a value from controller and send it to mysql along with the other input that I get from the view. How do I supposed to do it?
I tried using array_merge, tried to put them at the same variable and send it with Laravel's create but to no avail.
Here's the code
The Value
$id = 0;
$id++;
$time = "2019-01-01 00:00:00";
$date = new Carbon( $time );
$d = $date->format('Y');
$fmipa = 0;
$fkh = 0;
$lastNumber = 0;
$fk = $request->get('fakultas');
if($fk == 'FMIPA'){
$fmipa++;
$fk = 42;
if($fmipa < 10){
$fmipa="00". $fmipa;
$lastNumber = $fmipa;
}else if($fmipa < 100){
$fmipa="0" . $fmipa;
$lastNumber = $fmipa;
}else{
$fmipa = $fmipa;
$lastNumber = $fmipa;
}
}else{
$fkh++;
$fk = 41;
if($fkh < 10){
$fkh="00". $fkh;
$lastNumber = $fkh;
}else if($fkh < 100){
$fkh="0" . $fkh;
$lastNumber = $fkh;
}else{
$fkh = $fkh;
$lastNumber = $fkh;
}
}
$nim = $d . $fk . "101" . $lastNumber;
The Store() function
$validator = Validator::make($request->input(), array(
'nama' => 'required',
'alamat' => 'required',
'fakultas' => 'required',
));
if ($validator->fails()) {
return response()->json([
'error' => true,
'messages' => $validator->errors(),
], 422);
}
$mahasiswa = Dashboard::create($request->all() + ['nim' => $nim]);
return response()->json([
'error' => false,
'mhs' => $mahasiswa,
], 200);
I seperate them to make it easy to read, but in case you're wondering the value's code is right in the store() function.
I expect it to post the value to the database and yet what I got from XHR's Response is
500 Internal Server Error
and
SQLSTATE[HY000]: General error: 1364 Field 'nim' doesn't have a default value