在保存带有成功消息的数据后,我试图从控制器重定向到上一页。它将变为空白页面,如果我重新加载该页面,则会显示会话消息。但直接将其不重定向到上一页。我使用了建议的其他重定向方法,但仍然遇到相同的问题。我使用了以下代码:
控制器
public function saveMachine(Request $request) {
$number_of_lockers = $request->number_of_lockers;
if($number_of_lockers == '32') {
$a_machine = A_Machine::all();
if($a_machine->isEmpty()) {
$this->saveFirstMachine(32, $request);
} else {
$this->saveNewMachine(32, $request);
}
} else if($number_of_lockers == '64') {
$a_machine = B_Machine::all();
if($a_machine->isEmpty()) {
$this->saveFirstMachine(64, $request);
} else {
$this->saveNewMachine(64, $request);
}
} else if($number_of_lockers == '128') {
$a_machine = C_Machine::all();
if($a_machine->isEmpty()) {
$this->saveFirstMachine(128, $request);
} else {
$this->saveNewMachine(128, $request);
}
}
}
private function saveNewMachine($number_of_lockers, $request) {
$machine = null;
$machine_code = '';
$locker_start=0;
$locker_end=0;
if($number_of_lockers == 32) {
$machine = new A_Machine;
$date = \Carbon\Carbon::now()->format('Y-m-dH:i:s');
$machine_code = 'm032'.str_replace(array('20', '-', ':'), array('', '', ''), $date);
$last_record = $machine->all()->last();
$last_locker_end = $last_record->locker_end;
$locker_start = $last_locker_end + 1;
$locker_end = $locker_start + 31;
}
if($number_of_lockers == 64) {
$machine = new B_Machine;
$date = \Carbon\Carbon::now()->format('Y-m-dH:i:s');
$machine_code = 'm064'.str_replace(array('20', '-', ':'), array('', '', ''), $date);
$last_record = $machine->all()->last();
$last_locker_end = $last_record->locker_end;
$locker_start = $last_locker_end + 1;
$locker_end = $locker_start + 63;
}
if($number_of_lockers == 128) {
$machine = new C_Machine;
$date = \Carbon\Carbon::now()->format('Y-m-dH:i:s');
$machine_code = 'm128'.str_replace(array('20', '-', ':'), array('', '', ''), $date);
$last_record = $machine->all()->last();
$last_locker_end = $last_record->locker_end;
$locker_start = $last_locker_end + 1;
$locker_end = $locker_start + 127;
}
$machine->vendor_id = $request->vendor_id;
$machine->locker_start = $locker_start;
$machine->locker_end = $locker_end;
$machine->machine_code = $machine_code;
$machine->qr_code = 'test code';
$machine->temperature = '123';
$machine->humidity = '123';
$machine->fan_status = 'off';
if($machine->save()) {
$saved = $this->saveLocker($number_of_lockers, $machine->id);
if($saved) {
return redirect()->route('create_machine')->with('success','A New Machine with '.$number_of_lockers.' Lockers Added Successfully');
}
}
}
private function saveLocker($number_of_lockers, $machine_id) {
$lockers = [];
if($number_of_lockers == 32) {
for ($i=0; $i < 32 ; $i++) {
$lockers[$i]['machine_id'] = $machine_id;
$lockers[$i]['product_id'] = 0;
$lockers[$i]['created_at'] = now();
$lockers[$i]['updated_at'] = now();
}
return DB::table('a_machine_lockers')->insert($lockers);
}
if($number_of_lockers == 64) {
for ($i=0; $i < 64 ; $i++) {
$lockers[$i]['machine_id'] = $machine_id;
$lockers[$i]['product_id'] = 0;
$lockers[$i]['created_at'] = now();
$lockers[$i]['updated_at'] = now();
}
return DB::table('b_machine_lockers')->insert($lockers);
}
if($number_of_lockers == 128) {
for ($i=0; $i < 128 ; $i++) {
$lockers[$i]['machine_id'] = $machine_id;
$lockers[$i]['product_id'] = 0;
$lockers[$i]['created_at'] = now();
$lockers[$i]['updated_at'] = now();
}
return DB::table('c_machine_lockers')->insert($lockers);
}
}
刀片
@if(session('success'))
<div class="alert alert-success alert-dismissible fade show mb-4" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close">
<span aria-hidden="true">×</span>
</button>
<i class="fa fa-check mx-2"></i>
{{ session('success') }}
</div>
@endif
上一页路线基本上是:
Route::get('create_machine', 'AdminController@createMachine')->name('create_machine');
具有控制器方法:
public function createMachine() {
$vendors = \App\Vendor::all();
return view('admin.create_machine', compact('vendors'));
}
N:B:我在我的项目中使用多重身份验证。
答案 0 :(得分:3)
尝试使用路由重定向:
return redirect()->route('create_machine')->with('success','The First Machine with '.$number_of_lockers.' Lockers Added Successfully');
在视图中确实更改消息显示,如:
@if(session()->has('success'))
<div class="alert alert-success">
{{ session()->get('success') }}
</div>
@endif
@if(session()->has('error'))
<div class="alert alert-danger">
{{ session()->get('error') }}
</div>
@endif
似乎您还没有处理else case
的保存方法。所以也要添加它。
赞:
private function saveFirstMachine($number_of_lockers, $request) {
...
if($machine->save()) {
$saved = $this->saveLocker($number_of_lockers, $machine->id);
if($saved) {
return redirect()->route('create_machine')->with('success','The First Machine with '.$number_of_lockers.' Lockers Added Successfully');
}
}
return redirect()->route('create_machine')->with('error','There is something wrong! Please try again.');
}
已编辑:
更改saveMachine
,添加return
,如
public function saveMachine(Request $request) {
$number_of_lockers = $request->number_of_lockers;
if($number_of_lockers == '32') {
$a_machine = A_Machine::all();
if($a_machine->isEmpty()) {
return $this->saveFirstMachine(32, $request);
} else {
return $this->saveNewMachine(32, $request);
}
} else if($number_of_lockers == '64') {
$a_machine = B_Machine::all();
if($a_machine->isEmpty()) {
return $this->saveFirstMachine(64, $request);
} else {
return $this->saveNewMachine(64, $request);
}
} else if($number_of_lockers == '128') {
$a_machine = C_Machine::all();
if($a_machine->isEmpty()) {
return $this->saveFirstMachine(128, $request);
} else {
return $this->saveNewMachine(128, $request);
}
}
return redirect()->route('create_machine')->with('error','There is something wrong! Please try again.');
}
答案 1 :(得分:0)
尝试
return redirect()->back()->with('success','your message');
答案 2 :(得分:0)
@if(Session::has('message'))
@if( 'danger' == Session::get('message_type') )
<div class="alert alert-danger alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<b>ERROR!</b> {{ Session::get('message') }}
</div>
@elseif( 'success' == Session::get('message_type') )
<div class="alert alert-success alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<b>SUCCESSFUL!</b> {{ Session::get('message') }}
</div>
@elseif( 'warning' == Session::get('message_type') )
<div class="alert alert-warning alert-dismissable">
<button type="button" class="close" data-dismiss="alert">×</button>
<b>ALERT!</b> {{ Session::get('message') }}
</div>
@endif
@endif
控制器
public function __construct(Machine $machine)
{
$this->machine = $machine;
}
public function store (CreateSaveMachineRequest $request) { //CreateSaveMachineRequest Request for validation
$machine = $this->machine->create($request->except('_token'));
try {
if($machine){
return \Redirect::back()->with('message', 'machine Created successfully. ')
->with('message_type', 'success');
}
}
catch (\Exception $exception) {
return \Redirect::back()->with('message','machine Not Created')
->with('message_type','danger')
->withInput();
}
}
创建类似于alert.blade.php的刀片文件 @include(alert)到所有刀片文件