Redirect goes blank page without redirecting to route Laravel 5.8

by WahidSherief   Last Updated June 12, 2019 07:26 AM - source

I am trying to redirect to the previous page from the controller after saving data with a success message.It goes blank page and if i reload the page the session message appears. But directly its not redirecting to the previous page. I have used other redirect methods suggested but still get same issue. I have used following code:

controller


    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);
        }
    }

blade

@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

Basically the previous page route is :

Route::get('create_machine', '[email protected]')->name('create_machine');

which has the controller method:

public function createMachine() {  
        $vendors = \App\Vendor::all(); 
        return view('admin.create_machine', compact('vendors'));   
}

N:B: I am using multi-auth in my project.



Answers 1


Try with route redirect:

return redirect()->route('create_machine')->with('success','The First Machine with '.$number_of_lockers.' Lockers Added Successfully');

In view do change the message display like:

@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

Also it seems you have not handled the else case for the save menthod. so just add that too.

Like:

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.');
}

Edited:

Change the saveMachine, add return like

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.');
}
Vikash Pathak
Vikash Pathak
June 12, 2019 05:11 AM

Related Questions


Class 'App\Order' not found

Updated January 19, 2018 14:26 PM

Laravel Routing 404 Error on First Redirect

Updated February 12, 2019 00:26 AM



Remove csrf token only for single method - Laravel

Updated February 12, 2019 14:26 PM