I'm using laravel to my project, I need to generate no_track from the id of the record,created_at->hour and created_at->minute. I can save the data to the database but I can't save the data to the database after creating the no_track.. the error messages said array_key_exists(): The first argument should be either a string or an integer. this is I attached an image of the error message.. when I save the data to the database I want to get no_track from the id of the record, hour and minute and save the no_track to the no_track field.
This my form code:
<div class="row">
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-heading" style="background-color: #2a3b48; color: #ffffff"><center>Entri Data Gangguan</center></div>
<div class="panel-body">
<form class="form-horizontal" role="form" method="POST" action="">
<div class="form-group">
<label for="id_jenis" class="col-md-4 control-label">Jenis Gangguan</label>
<div class="col-md-6">
<select name="id_jenis" id="id_jenis" class="form-control jgangguan" value="" required>
<option disabled="true" selected="true">- Pilihan -</option>
@forelse($jgangguan as $jgangguan)
<option value=""></option>
@empty
@endforelse
</select>
@if ($errors->has('id_jenis'))
<span class="help-block">
<strong></strong>
</span>
@endif
</div>
</div>
<div class="form-group cgangguan">
<label for="id_app_jar" class="col-md-4 control-label"></label>
<div class="col-md-6">
<select name="id_app_jar" id="id_app_jar" class="form-control jappjar" value="" required>
<option value="0" disabled selected>- Pilihan -</option>
</select>
@if ($errors->has('id_app_jar'))
<span class="help-block">
<strong></strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<label for="detail" class="col-md-4 control-label"></label>
<div class="col-md-6">
<textarea name="detail" id="detail" class="form-control" value="" placeholder="Detail Gangguan">
</textarea>
@if ($errors->has('detail'))
<span class="help-block">
<strong></strong>
</span>
@endif
</div>
</div>
<div class="form-group">
<div class="col-md-6 col-md-offset-4">
<button type="submit" class="btn btn-danger col-md-3">
Lapor
</button>
</div>
</div>
</form>
<script src=""></script>
<script type="text/javascript">
$(document).ready(function () {
$(document).on('change','.jgangguan', function () {
//console.log('its change');
var id_jenis=$(this).val();
//console.log(id_jenis);
var div=$(this).parents();
var op="";
switch (id_jenis){
case '1' : $.ajax({
type:'get',
url:'{!! URL::to('data-jar') !!}',
data:{'id':id_jenis},
success:function (data) {
//console.log('success');
//console.log(data);
//console.log(data);
op+='<option value="0" selected disabled>- Pilih Jaringan -</option>';
for(i=0;i<data.length;i++){
op+='<option value="'+data[i].id+'">'+data[i].nama_jaringan+'</option>';
}
div.find('.jappjar').html("");
div.find('.jappjar').append(op);
},
error:function () {
}
});
break;
case '2' : $.ajax({
type:'get',
url:'{!! URL::to('data-app') !!}',
data:{'id':id_jenis},
success:function (data) {
//console.log('success');
//console.log(data);
//console.log(data);
op+='<option value="0" selected disabled>- Pilih Aplikasi -</option>';
for(i=0;i<data.length;i++){
op+='<option value="'+data[i].id+'">'+data[i].nama+'</option>';
}
div.find('.jappjar').html("");
div.find('.jappjar').append(op);
},
error:function () {
}
});
break;
}
});
});
</script>
</div>
</div>
</div>
</div>
This my controller:
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
$jgangguan = JenisLayanan::all();
$app = Aplikasi::all();
$jar = Jaringan::all();
return view('lapor.create', compact('jgangguan','app','jar'));
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$this->validate($request,[
'id_jenis' => 'required',
'id_app_jar' =>'required'
]);
//$status = Status::all();
$status = 1;
$codeH = substr(strftime("%H", time()),0);
$codeM = substr(strftime("%M", time()),0);
$codeS = substr(strftime("%S", time()),0);
$id = Auth::user()->id;
$id_app_jar = $request->id_app_jar;
$id_jenis = $request->id_jenis;
$gangguan = new Gangguan();
$gangguan->id_pelapor = $id;
$gangguan->id_jenis = $id_jenis;
$gangguan->id_aplikasi_atau_jaringan = $id_app_jar;
$gangguan->detail = $request->detail;
$gangguan->id_status = $status;
$gangguan->no_track = $codeH.$codeM;
$gangguan->save();
$id = $gangguan->id;
$jam = $gangguan->created_at->hour;
$menit = $gangguan->created_at->minute;
$track = $id.$jam.$menit;
$gangguan = Gangguan::findOrFail($id);
$gangguan->update(array(
$gangguan->no_track = $track
));
return redirect()
->route('dashboard')
->with('message',$track);
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id)
{
//
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id)
{
//
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id)
{
//
}
public function dataJar(){
$jar = Jaringan::all();
return response()->json($jar);
}
public function dataApp(){
$app = Aplikasi::all();
return response()->json($app);
}
This my table structure:
via Nur Raga