4

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 ?

8
  • 1
    We need your view as well. Just sounds like a generic array error. start_time in your $pta variable doesn't exist. It's not being sent properly with the request. Commented Nov 7, 2016 at 3:45
  • @SteveBauman updated view form. Commented Nov 7, 2016 at 3:49
  • 1
    Are you using dev tools (specifically the network tab) and inspect the post or get request to make sure index is being posted with the request? This sounds like laravel is not getting the index during the request. Commented Nov 7, 2016 at 3:59
  • @Birdy sorry, i don't get it , what do you mean by dev tools ? Commented Nov 7, 2016 at 4:01
  • Web developer tools on your browser, for example Firefox would be tools > Web developer > Network (Ctrl+Shift+Q). Another option would be to add: return $request->all(); at the top of your saveHandler function so it puts the full request to your screen and see exactly whats being passed in the request. Commented Nov 7, 2016 at 4:03

2 Answers 2

2

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?

Sign up to request clarification or add additional context in comments.

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.