所以我有一个if-else函数,它会检查表单中应该是什么,所以当我提交表单时,控制器中的函数会执行if-else函数。所以它的类似,如果隐藏的输入字段中没有值,那么它会将用户重定向回到表单,但是当它这样做时,其他文本输入的所有值都会丢失,用户必须键入所有值再次。我不希望这种情况发生。相反,我希望保留所有其他文本输入。我怎么能这样做?
我的刀片:
<form id="form-project" role="form" autocomplete="off" action="{{action('Web\TravelController@store')}}" method="POST">
{{csrf_field()}}
<div class="card-bg">
<div class="back-btn">
<a href="{{action('Web\TravelController@index')}}">
<i class="fa fa-angle-left" aria-hidden="true"></i><span>Back</span>
</a>
</div>
<br>
<div class="form-group">
<div class="row">
<div class="col-md-12">
<div class="btn-group">
<a type="button" class="btn tab-btn active btn-tab-lg">Book a trip</a>
<a type="button" class="btn tab-btn btn-tab-lg" href="{{action('Web\TravelController@multiCity')}}">Multi-city</a>
</div>
</div>
</div>
<div class="add-space"></div>
<div class="row">
<div class="col-md-12">
<div class="btn-group">
<label class="btn tab-btn btn-tab-lg">
<input type="radio" name="is_return_trip" class="is_return_trip" value="0" onchange="hideReturn(this)">One-way
</label>
<label class="btn tab-btn active btn-tab-lg">
<input type="radio" name="is_return_trip" class="is_return_trip" value="1" checked onchange="hideReturn(this)">Return
</label>
</div>
</div>
</div>
<div class="extra-padding">
@if(request()->user()->hasRole('PA'))
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Request as</label>
<select class="form-control border-input" name="user_id" required>
<option value="{{$user->id}}">{{$user->name}}</option>
@foreach($user->bosses as $boss)
<option value="{{$boss->id}}">{{$boss->name}}</option>
@endforeach
</select>
<input type="hidden" name="pa_id" value="{{$user->id}}">
<input type="hidden" name="submitted_by_pa">
</div>
</div>
</div>
@else
<div class="row">
<div class="col-md-6">
<div class="form-group">
<input type="hidden" name="user_id" value="{{$user->id}}">
</div>
</div>
</div>
@endif
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Mode of Transport</label>
<select name="travel_data[transport_type_id]" class="form-control getForm select2" required>
<option value="">Select a transport type</option>
@foreach($transports as $transport)
<option value="{{$transport->id}}">{{$transport->name}}</option>
@endforeach
</select>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<div id="test">
<label>From</label>
<input type="text" class="form-control autocomplete border-input inputDatabaseName" placeholder="From" name="travel_data[from]" required>
<input type="hidden" class="field administrative_area_level_1" disabled="true" name="travel_data[from_state]">
<input type="hidden" class="field country" disabled="true" name="travel_data[from_country]">
</div>
</div>
</div>
<div class="col-md-6">
<div class="form-group">
<div id="test2">
<label>To</label>
<input type="text" class="form-control autocomplete border-input inputDatabaseName2" placeholder="To" name="travel_data[to]" required>
<input type="hidden" class="field administrative_area_level_1" disabled="true" name="travel_data[to_state]">
<input type="hidden" class="field country" disabled="true" name="travel_data[to_country]">
</div>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Departure Date</label>
<input type="text" class="form-control datepicker departure-date" placeholder="Departure date" name="travel_data[departure_date]" required>
</div>
</div>
<div class="col-md-6" id="return">
<div class="form-group">
<label>Return Date</label>
<input type="text" class="form-control datepicker return-date" placeholder="Return date" name="travel_data[return_date]">
</div>
</div>
</div>
<div class="row">
<div class="col-md-12">
<div class="button-toggle">
<label>Selected Dates</label>
</div>
<div class="btn-group">
<label class="btn tab-btn active btn-tab-lg" >
<input type="radio" name="travel_data[is_official_date]" class="is_official_date" value="1" checked onchange="showOfficialDates(this)">Official
</label>
<label class="btn tab-btn btn-tab-lg" >
<input type="radio" name="travel_data[is_official_date]" class="is_official_date" value="0" onchange="showOfficialDates(this)">Non-Official
</label>
</div>
</div>
</div>
<div class="row" id="officialdDates" style="display: none;">
<div class="col-lg-12 add-date">
<button class="btn alt-btn-black btn-xs alt-btn dateButton" type="button">
<i class="fa fa-plus"></i>
</button>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default">
<label>From</label>
<input type="text" placeholder="From" class="form-control datepicker fromOfficialDate" name="fromOfficialDate[]">
</div>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default">
<label>To</label>
<input type="text" placeholder="To" class="form-control datepicker toOfficialDate" name="toOfficialDate[]" >
</div>
</div>
</div>
<div class="rulesFormClone"></div>
<br>
<div class="row">
<div class="col-md-12">
<div class="button-toggle">
<label>Accommodation</label>
</div>
<div class="btn-group">
<label class="btn tab-btn active btn-tab-lg" >
<input type="radio" name="travel_data[accommodation]" class="accommodation" value="1" checked>Yes
</label>
<label class="btn tab-btn btn-tab-lg" >
<input type="radio" name="travel_data[accommodation]" class="accommodation" value="0">No
</label>
</div>
</div>
</div>
<br>
<div class="row">
<div class="col-md-6">
<div class="form-group">
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Travel Description</label>
<textarea maxlength="500" contentEditable="true" rows="10" cols="50" name="travel_data[description]" class="form-control border-input" placeholder="Travel description" required></textarea>
{{-- <input name="travel_data[description]" type="text" class="form-control border-input" placeholder="Travel Description" required> --}}
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Travel Preferences</label>
<input name="travel_data[travel_preferences]" type="text" class="form-control border-input" placeholder="Travel preferences" required>
</div>
</div>
</div>
<div class="row">
<div class="col-md-6">
<div class="form-group">
<label>Add Passengers</label>
<input name="travel_data[add_passengers]" type="text" class="form-control border-input" placeholder="Add passengers">
</div>
</div>
</div>
</div>
</div>
</div>
<div class="pull-right" >
<button class="submit-btn" name="status" value='S'>SAVE</button>
<button class="submit-btn text-gold" name="status" value='SB'>SUBMIT</button>
</div>
<div class="clearfix"></div>
<div class="clearfix"></div>
</form>
<div class="row" style="display: none;">
<div class="row datesForm">
<div class="col-lg-12 add-date">
<button class="btn alt-btn-black btn-xs alt-btn dateButton" type="button">
<i class="fa fa-plus"></i>
</button>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default">
<label>From</label>
<input type="text" placeholder="From" class="form-control datepicker fromOfficialDate" name="fromOfficialDate[]">
</div>
</div>
<div class="col-lg-6">
<div class="form-group form-group-default">
<label>To</label>
<input type="text" placeholder="To" class="form-control datepicker toOfficialDate" name="toOfficialDate[]" >
</div>
</div>
</div>
</div>
</div>
<div class="col-lg-4">
<div class="card-bg summary-side-bar">
<h4 class="title">Summary</h4>
<h5>Travel Type</h5>
<p class="traveltype">Return</p>
<h5>Travel Route</h5>
<p class="DatabaseName"></p>
<h5>Departure Date</h5>
<p class="departure-date1"></p>
<h5>Return Date</h5>
<p class="return-date1"></p>
</div>
</div>
</div>
</div>
</body>
我的功能是:
public function store(Request $request)
{
$user = Auth::user();
$today = date('Y-m-d');
$inputs = $request->all();
$inputTravel_data = $request->input('travel_data');
if($request->has('is_return_trip'))
{
$travelTitle = $inputTravel_data['from'].' - '.$inputTravel_data['to'];
$multiCity = 0;
$is_return_trip = $inputs['is_return_trip'];
}else{
$count = count($inputs['travelLeg']);
$title = $inputTravel_data['from'][0];
for ($i = 1; $i < $count; $i++)
{
$title .= ' - ' . $inputTravel_data['from'][$i];
}
$travelTitle = $title. ' - ' .$inputTravel_data['to'][$count-1];
$multiCity = 1;
$is_return_trip = 0;
}
if(!$inputTravel_data['from_country']){
return redirect()->back();
}else{
if(array_key_exists('id', $inputs) && $inputs['id'])
{
$travel = Travel::where('id',$this->decode($inputs['id']))->first();
if(!$travel)
return $this->error('message','No travel found for this user');
$travel->update([
'status' => $inputs['status'],
'title' => $travelTitle,
'is_multicity' => $multiCity,
'is_return_trip' => $is_return_trip,
'company_id' => $user->company_id,
'submitted_by_pa' => $inputs['user_id'] != Auth::user()->id ? 0 : 1,
'pa_id' => $inputs['user_id'] != Auth::user()->id ? $inputs['pa_id'] : null,
]);
if($inputs['status'] == 'SB' && $inputs['user_id'] == Auth::user()->id){
foreach($travel->travel_data()->where('removed',0)->get() as $t) {
$t->update(['removed'=> 1]);
}
}elseif($inputs['status'] == 'S'){
foreach($travel->travel_data()->where('removed',0)->get() as $t) {
$t->delete();
}
}
if($request->has('is_return_trip'))
{
$travelLeg = new TravelLeg;
$travelLeg->travel_id = $travel->id;
$travelLeg->transport_type_id = $inputTravel_data['transport_type_id'];
$travelLeg->accommodation = $inputTravel_data['accommodation'];
$travelLeg->travel_region_id = $inputTravel_data['travel_region_id'];
$travelLeg->travel_purpose = TravelPurpose::find($inputTravel_data['travel_purpose_id'])->name;
$travelLeg->description = $inputTravel_data['description'];
$travelLeg->travel_prefernces = $inputTravel_data['travel_preferences'];
$travelLeg->add_passengers = $inputTravel_data['add_passengers'];
$travelLeg->from = $inputTravel_data['from'];
$travelLeg->from_state = $inputTravel_data['from_state'] ?? '';
$travelLeg->from_country = $inputTravel_data['from_country'] ?? '';
$travelLeg->to = $inputTravel_data['to'];
$travelLeg->to_state = $inputTravel_data['to_state'] ?? '';
$travelLeg->to_country = $inputTravel_data['to_country'] ?? '';
$travelLeg->departure_date = date('Y-m-d', strtotime(str_replace('/', '-', $inputTravel_data['departure_date'])));
if (!empty($inputTravel_data['return_date']))
{
$travelLeg->return_date = date('Y-m-d', strtotime(str_replace('/', '-', $inputTravel_data['return_date'])));
}
$travelLeg->title = 'Travel Leg 1';
$travelLeg->is_official_date = $inputTravel_data['is_official_date'];
$travelLeg->mode_of_transport = TransportType::find($inputTravel_data['transport_type_id'])->name;
$travelLeg->region = TravelRegion::find($inputTravel_data['travel_region_id'])->name;
$travelLeg->travel_purpose_id = $inputTravel_data['travel_purpose_id'];
$travelLeg->processed = 0;
$travelLeg->save();
if ($travelLeg->is_official_date == self::IS_OFFICIALDATE)
{
$travelLeg->official_date()->delete();
}
if ($travelLeg->is_official_date == self::IS_NOT_OFFICIALDATE)
{
$travelLeg->official_date()->delete();
for ($i = 0; $i < count($inputs['fromOfficialDate']); $i++)
{
$travelDate = new TravelDate;
$travelDate->travel_leg_id = $travelLeg->id;
$travelDate->from = date('Y-m-d', strtotime(str_replace('/', '-', $inputs['fromOfficialDate'][$i])));
$travelDate->to = date('Y-m-d', strtotime(str_replace('/', '-', $inputs['toOfficialDate'][$i])));
$travelDate->save();
}
}
}
else
{
for ($i = 0; $i < $count; $i++)
{
$travelDataFromState ='';
$temporaryIndex = $i;
if ($inputTravel_data['from_state'][$temporaryIndex] == '') {
if (($temporaryIndex-1) <= 0) {
$temporaryIndex = 0;
} else {
$temporaryIndex = $temporaryIndex - 1;
}
$travelDataFromState = $inputTravel_data['to_state'][$temporaryIndex];
} else {
$travelDataFromState = $inputTravel_data['from_state'][$temporaryIndex] ?? '';
}
$travelDataFromCountry = '';
if ($inputTravel_data['from_country'][$temporaryIndex] == '') {
if (($temporaryIndex-1) <= 0) {
$temporaryIndex = 0;
} else {
$temporaryIndex = $temporaryIndex - 1;
}
$travelDataFromCountry = $inputTravel_data['to_country'][$temporaryIndex];
} else {
$travelDataFromCountry = $inputTravel_data['from_country'][$temporaryIndex] ?? '';
}
$travelLeg = new TravelLeg;
$travelLeg->travel_id = $travel->id;
$travelLeg->transport_type_id = $inputTravel_data['transport_type_id'][$i];
$travelLeg->accommodation = $inputTravel_data['accommodation'][$i];
$travelLeg->travel_region_id = $inputTravel_data['travel_region_id'][$i];
$travelLeg->travel_purpose = TravelPurpose::find($inputTravel_data['travel_purpose_id'][$i])->name;
$travelLeg->description = $inputTravel_data['description'][$i];
$travelLeg->travel_prefernces = $inputTravel_data['travel_preferences'][$i];
$travelLeg->add_passengers = $inputTravel_data['add_passengers'][$i];
$travelLeg->from = $inputTravel_data['from'][$i];
$travelLeg->from_state = $travelDataFromState;
$travelLeg->from_country = $travelDataFromCountry;
$travelLeg->to = $inputTravel_data['to'][$i];
$travelLeg->to_state = $inputTravel_data['to_state'][$i] ?? '';
$travelLeg->to_country = $inputTravel_data['to_country'][$i] ?? '';
$travelLeg->departure_date = date('Y-m-d', strtotime(str_replace('/', '-', $inputTravel_data['departure_date'][$i])));
$travelLeg->title = 'Travel Leg ' . $inputs['travelLeg'][$i+1];
$travelLeg->is_official_date = $inputTravel_data['is_official_date'][$i];
$travelLeg->mode_of_transport = TransportType::find($inputTravel_data['transport_type_id'][$i])->name;
$travelLeg->region = TravelRegion::find($inputTravel_data['travel_region_id'][$i])->name;
$travelLeg->travel_purpose_id = $inputTravel_data['travel_purpose_id'][$i];
$travelLeg->processed = 0;
$travelLeg->save();
if ($travelLeg->is_official_date == self::IS_OFFICIALDATE)
{
$travelLeg->official_date()->delete();
}
if ($travelLeg->is_official_date == self::IS_NOT_OFFICIALDATE)
{
$travelLeg->official_date()->delete();
$dates = $inputs['OfficialDate'][$inputs['travelLeg'][$i+1]];
for ($j = 0; $j < count($dates['from']); $j++)
{
$fromODate = new TravelDate;
$fromODate->travel_leg_id = $travelLeg->id;
$fromODate->from = date('Y-m-d', strtotime(str_replace('/', '-', $dates['from'][$j])));
$fromODate->to = date('Y-m-d', strtotime(str_replace('/', '-', $dates['to'][$j])));
$fromODate->save();
}
}
}
}
}
else {
if($user->id != $inputs['user_id']){
$user = User::find($inputs['user_id']);
}
$travel = $user->travels()->create([
'status' => $inputs['status'],
'title' => $travelTitle,
'is_multicity' => $multiCity,
'is_return_trip' => $is_return_trip,
'company_id' => $user->company_id,
'submitted_by_pa' => $inputs['user_id'] != Auth::user()->id ? 0 : 1,
'pa_id' => $inputs['user_id'] != Auth::user()->id ? $inputs['pa_id'] : null,
]);
$this->generateRef($travel);
if($request->has('is_return_trip'))
{
$travelLeg = new TravelLeg;
$travelLeg->travel_id = $travel->id;
$travelLeg->transport_type_id = $inputTravel_data['transport_type_id'];
$travelLeg->accommodation = $inputTravel_data['accommodation'];
$travelLeg->travel_region_id = $inputTravel_data['travel_region_id'];
$travelLeg->travel_purpose = TravelPurpose::find($inputTravel_data['travel_purpose_id'])->name;
$travelLeg->description = $inputTravel_data['description'];
$travelLeg->travel_prefernces = $inputTravel_data['travel_preferences'];
$travelLeg->add_passengers = $inputTravel_data['add_passengers'];
$travelLeg->from = $inputTravel_data['from'];
$travelLeg->from_state = $inputTravel_data['from_state'] ?? '';
$travelLeg->from_country = $inputTravel_data['from_country'] ?? '';
$travelLeg->to = $inputTravel_data['to'];
$travelLeg->to_state = $inputTravel_data['to_state'] ?? '';
$travelLeg->to_country = $inputTravel_data['to_country'] ?? '';
$travelLeg->departure_date = date('Y-m-d', strtotime(str_replace('/', '-', $inputTravel_data['departure_date'])));
if (!empty($inputTravel_data['return_date']))
{
$travelLeg->return_date = date('Y-m-d', strtotime(str_replace('/', '-', $inputTravel_data['return_date'])));
}
$travelLeg->title = 'Travel Leg 1';
$travelLeg->is_official_date = $inputTravel_data['is_official_date'];
$travelLeg->mode_of_transport = TransportType::find($inputTravel_data['transport_type_id'])->name;
$travelLeg->region = TravelRegion::find($inputTravel_data['travel_region_id'])->name;
$travelLeg->travel_purpose_id = $inputTravel_data['travel_purpose_id'];
$travelLeg->processed = 0;
$travelLeg->save();
if ($travelLeg->is_official_date == self::IS_NOT_OFFICIALDATE)
{
for ($i = 0; $i < count($inputs['fromOfficialDate']); $i++)
{
$travelDate = new TravelDate;
$travelDate->travel_leg_id = $travelLeg->id;
$travelDate->from = date('Y-m-d', strtotime(str_replace('/', '-', $inputs['fromOfficialDate'][$i])));
$travelDate->to =date('Y-m-d', strtotime(str_replace('/', '-', $inputs['toOfficialDate'][$i])));
$travelDate->save();
}
}
}
else
{
for ($i = 0; $i < $count; $i++)
{
$travelDataFromState ='';
$temporaryIndex = $i;
if ($inputTravel_data['from_state'][$temporaryIndex] == '') {
if (($temporaryIndex-1) <= 0) {
$temporaryIndex = 0;
} else {
$temporaryIndex = $temporaryIndex - 1;
}
$travelDataFromState = $inputTravel_data['to_state'][$temporaryIndex];
} else {
$travelDataFromState = $inputTravel_data['from_state'][$temporaryIndex] ?? '';
}
$travelDataFromCountry = '';
if ($inputTravel_data['from_country'][$temporaryIndex] == '') {
if (($temporaryIndex-1) <= 0) {
$temporaryIndex = 0;
} else {
$temporaryIndex = $temporaryIndex - 1;
}
$travelDataFromCountry = $inputTravel_data['to_country'][$temporaryIndex];
} else {
$travelDataFromCountry = $inputTravel_data['from_country'][$temporaryIndex] ?? '';
}
$travelLeg = new TravelLeg;
$travelLeg->travel_id = $travel->id;
$travelLeg->transport_type_id = $inputTravel_data['transport_type_id'][$i];
$travelLeg->accommodation = $inputTravel_data['accommodation'][$i];
$travelLeg->travel_region_id = $inputTravel_data['travel_region_id'][$i];
$travelLeg->travel_purpose = TravelPurpose::find($inputTravel_data['travel_purpose_id'][$i])->name;
$travelLeg->description = $inputTravel_data['description'][$i];
$travelLeg->travel_prefernces = $inputTravel_data['travel_preferences'][$i];
$travelLeg->add_passengers = $inputTravel_data['add_passengers'][$i];
$travelLeg->from = $inputTravel_data['from'][$i];
$travelLeg->from_state = $travelDataFromState;
$travelLeg->from_country = $travelDataFromCountry;
$travelLeg->to = $inputTravel_data['to'][$i];
$travelLeg->to_state = $inputTravel_data['to_state'][$i] ?? '';
$travelLeg->to_country = $inputTravel_data['to_country'][$i] ?? '';
$travelLeg->departure_date = date('Y-m-d', strtotime( str_replace('/', '-', $inputTravel_data['departure_date'][$i])));
$travelLeg->title = 'Travel Leg ' . $inputs['travelLeg'][$i+1];
$travelLeg->is_official_date = $inputTravel_data['is_official_date'][$i];
$travelLeg->mode_of_transport = TransportType::find($inputTravel_data['transport_type_id'][$i])->name;
$travelLeg->region = TravelRegion::find($inputTravel_data['travel_region_id'][$i])->name;
$travelLeg->travel_purpose_id = $inputTravel_data['travel_purpose_id'][$i];
$travelLeg->processed = 0;
$travelLeg->save();
if ($travelLeg->is_official_date == self::IS_NOT_OFFICIALDATE)
{
$dates = $inputs['OfficialDate'][$inputs['travelLeg'][$i+1]];
for ($j = 0; $j < count($dates['from']); $j++)
{
$fromODate = new TravelDate;
$fromODate->travel_leg_id = $travelLeg->id;
$fromODate->from = date('Y-m-d', strtotime(str_replace('/', '-', $dates['from'][$j])));
$fromODate->to = date('Y-m-d', strtotime(str_replace('/', '-', $dates['to'][$j])));
$fromODate->save();
}
}
}
}
}
if($inputs['status'] == 'SB')
{
$pivotT = \DB::table('travel_approver_inboxes')
->where('travel_id', $travel->id)
->where('removed',0)
->get();
foreach($pivotT as $p) {
$travel->approver()
->newPivotStatement()
->where('id', $p->id)->update(['removed'=> 1]);
}
(new TravelApprover($travel))->assign();
$travel->update(['submitted_date' => $today,'remark'=> null]);
$travel->fresh();
// (new Notification)->addNotification($travel,'travel');
// dd($travel->approver->last());
}
return redirect(action('Web\TravelController@index'));
}
}
所以主要的重定向就在这里:
if(!$inputTravel_data['from_country']){
return redirect()->back();
答案 0 :(得分:0)
有时您可能希望将用户重定向到之前的位置,例如提交的表单无效时。您可以使用全局后退辅助函数来完成此操作。由于此功能使用会话,因此请确保调用后退功能的路由正在使用Web中间件组或已应用所有会话中间件:
Route::post('user/profile', function () {
return back()->withInput();
});
答案 1 :(得分:0)
我建议你仔细阅读文档,因为它在那里清楚而简单地解释过:https://laravel.com/docs/5.6/requests#old-input