Reputation: 499
I had 2 tables . driver and part_time_available, when I select driver type parttime it'll show part_time_available field. the problem is I can't save.
it throws this error : Undefined index: start_time
here's my save controller code so far :
public function saveHandler(Request $request, $obj)
{
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
foreach($request->parttimeAvailabilities as $pta) {
\Log::info($pta);
if (empty($pta['id'])) {
$parttimeAvailability = new PartTimeAvailability();
}
else {
$parttimeAvailability = PartTimeAvailability::find($pta['id']);
}
$parttimeAvailability->driver()->associate($obj);
$pta['driver_id'] = isset($pta['driver_id']);
$parttimeAvailability->day = $pta['day'];
$parttimeAvailability->start_time = $pta['start_time'];
$parttimeAvailability->end_time = $pta['end_time'];
$parttimeAvailability->available = isset($pta['available']);
$parttimeAvailability->save();
};
$obj->save();
if (!$parttimeAvailability->save()) {
throw new ValidationException($parttimeAvailability->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
here's my view form code :
<?php $index = 0; ?>
@foreach($dayOfWeek as $key => $day )
<div class="parttime">
<div class="row">
<div class="col-xs-2">
<div class="form-group">
{!! Form::text('parttimeAvailabilities['.$index.'][day]',$day, ['class' => 'form-control','disabled'])!!}
{!! Form::hidden('parttimeAvailabilities['.$index.'][day]',$key, ['class' => 'form-control'])!!}
{!! Form::hidden('parttimeAvailabilities['.$index.'][id]',null) !!}
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
{!! Form::text('parttimeAvailabilities['.$index.'][start_time]', null, ['class' => 'form-control start_time','placeholder' => 'Start time'])!!}
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
{!! Form::text('parttimeAvailabilities['.$index.'][end_time]', null, ['class' => 'form-control end_time','placeholder' => 'End time'])!!}
</div>
</div>
<div class="col-xs-2">
<div class="form-group">
{!! Form::text('parttimeAvailabilities['.$index.'][hours]', null, ['id' => 'hours','class' => 'form-control', 'readonly'])!!}
</div>
</div>
<div class="col-xs-2 text-center">
<div class="form-group">
{!! Form::checkbox('parttimeAvailabilities['.$index.'][available]')!!}
</div>
</div>
</div>
</div>
<?php $index++; ?>
@endforeach
any idea ?
Upvotes: 4
Views: 11891
Reputation: 2437
In my case, Laravel threw a Undefined index:'some_field'
exception,
because I accessed the field value(some_field)
to evaluate it, but that field is not sent by the client app.
So in this case, we have to use function isset($response['some_field'])
before accessing it. I hope this answer may help someone.
Upvotes: 1
Reputation: 773
After our chat here is my propsal, It may work just from a slight change on your pta['start_time'] array value.
public function saveHandler(Request $request, $obj)
{
try {
DB::beginTransaction();
$obj->fill($request->all());
if (!$obj->save()) {
throw new ValidationException($obj->errors());
}
foreach($request->parttimeAvailabilities as $pta) {
\Log::info($pta);
if (empty($pta['id'])) {
$parttimeAvailability = new PartTimeAvailability();
}
else {
$parttimeAvailability = PartTimeAvailability::find($pta['id']);
}
$parttimeAvailability->driver()->associate($obj);
$pta['driver_id'] = isset($pta['driver_id']);
$parttimeAvailability->day = $pta['day'];
$parttimeAvailability->start_time = isset($pta['start_time']) ? $pta['start_time'] : '00:00:00';
$parttimeAvailability->end_time = $pta['end_time'];
$parttimeAvailability->available = isset($pta['available']);
$parttimeAvailability->save();
};
$obj->save();
if (!$parttimeAvailability->save()) {
throw new ValidationException($parttimeAvailability->errors());
}
DB::commit();
return $this->sendSuccessResponse($request);
} catch (ValidationException $e) {
DB::rollback();
\Log::error($e->errors);
return $this->sendErrorResponse($request, $e->errors);
} catch (Exception $e) {
DB::rollback();
\Log::error($e->getMessage());
return $this->sendErrorResponse($request,'Unable to process. Please contact system Administrator');
}
}
You said you believe the issue is from your $pta['start_time'] being empty / null on the post request well you can use the operator to check if isset and if the value isset then use it and if not use a blank value as your database allows for nullable entries on that specific value.
Give it a shot and let me know, Hopefully it fixes the issue if not ill see about helping the best i can, Im no expert though :)
I have updated this answer due to your validation rules?
Upvotes: 2