未定义索引:Laravel
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/40457494/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Undefined index: Laravel
提问by Andrew Vanusi
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.
我有 2 张桌子。驱动程序和part_time_available,当我选择驱动程序类型parttime 时,它会显示part_time_available 字段。问题是我无法保存。
it throws this error : Undefined index: start_time
它抛出这个错误:未定义索引: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 ?
任何的想法 ?
采纳答案by Birdy
After our chat here is my propsal, It may work just from a slight change on your pta['start_time'] array value.
在我们这里的聊天之后是我的建议,它可能只需要稍微改变你的 pta['start_time'] 数组值。
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.
您说您认为问题出在您的 $pta['start_time'] 在 post 请求中为空/空,您可以使用运算符检查是否 isset,如果值 isset 然后使用它,如果不使用空值作为您的数据库允许该特定值的可为空条目。
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?
由于您的验证规则,我已更新此答案?
回答by Rajesh.k
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.
在我的例子中,Laravel 抛出了一个Undefined index:'some_field'
异常,因为我访问了该字段value(some_field)
来评估它,但该字段不是由客户端应用程序发送的。
So in this case, we have to use function isset($response['some_field'])
before accessing it. I hope this answer may help someone.
所以在这种情况下,我们必须function isset($response['some_field'])
在访问它之前使用它。我希望这个答案可以帮助某人。