我有一种形式,在其中输入两个字段Activity_datetime_from和Activity_datetime_to。问题是,当我更新表格时,如果我更新这两个字段,则更新功能工作正常,但是当我更新其他字段而不是这两个字段时,它会自动将这两个字段更新为0000-00-00 00:00:00 。这是我的更新函数的代码:
public function update(UpdateActivity $request, $id)
{
$activity = Activity::find($id);
if(!$activity) {
return response()->json(['message'=>'Data not found', 404]);
}
$activity->update($request->except(['activity_type', 'activity_picture']));
if($request->hasFile('activity_picture')) {
$activity_picture = $request->file('activity_picture');
$activity_picture_name = time().'.'.$activity_picture->getClientOriginalExtension();
$path = public_path('Storage/ActivityImages');
$activity_picture->move($path, $activity_picture_name);
$activity->activity_picture = 'Storage/ActivityImages/'.$activity_picture_name;
}
if ($request->has('activity_address')) {
$location = $request->input('activity_address');
$client = new \GuzzleHttp\Client();
$geocoder = new Geocoder($client);
$geocoder->setApiKey(config('geocoder.key'));
$address = $geocoder->getCoordinatesForAddress($location);
$latitude = $address['lat'];
$longitude = $address['lng'];
}
$activity->latitude = $latitude;
$activity->longitude = $longitude;
$activity->save();
return redirect()->route('activities.index')
->with('success','Activity updated successfully');
}
这是我的视图edit.blade.php
<div class="form-group">
{!!Form::label('activity_datetime_from','Activity From*:',['class'=>'col-md-2'])!!}
<div class="col-md-10">
<p>{{$activity->activity_datetime_from}}</p>
{!!Form::date('activity_datetime_from', null, ['class'=>'form-control','activity_datetime_from'])!!}
<!-- {!!Form::text('dateTime-local', null, ['class'=>'form-control',])!!} -->
</div>
</div>
<div class="form-group">
{!!Form::label('activity_datetime_to','Activity To*:',['class'=>'col-md-2'])!!}
<div class="col-md-10">
<p>{{$activity->activity_datetime_to}}</p>
{!!Form::date('activity_datetime_to', null, ['class'=>'form-control','activity_datetime_to'])!!}
</div>
</div>
<div class="form-group">
{!!Form::label('activity_address','Address*:',['class'=>'col-md-2'])!!}
<div class="col-md-10">
{!!Form::text('activity_address', null, ['class'=>'form-control','required'])!!}
</div>
</div>
我想如果我将更新除这两个字段以外的其他字段,则不会更改它包含的现有值。请提前帮助。
答案 0 :(得分:0)
替换
{!!Form::date('activity_datetime_from', null, ['class'=>'form-control','activity_datetime_from'])!!}
作者
{!!Form::date('activity_datetime_from', $activity->activity_datetime_from, ['class'=>'form-control','activity_datetime_from'])!!}