0

I have a simple Lavel 4 project and I am trying to do a simple redirect to the previous page after the information has been submitted. However, when inserting Redirect::back() it's only refreshing the current page. How can I get it to redirect properly to the previous page it was on?

Controller

    public function saveCustomer($id) { 

    $zip_code = DB::table('zip_codes')
                    ->where('zip_code', Input::get('user_zip_code'))
                    ->first();  

    if (!empty(Input::get('user_id'))) {
        $user_id = Input::get('user_id');

        $user = User::find($user_id);

        $user->user_zip_code = $zip_code->zip_code;

        $user->office = $zip_code->office;

        $user->save();

    } else {
        $user_id = NULL;
    }       

    $userdata = [
                'user_id' => $user_id,
                'first_name' => Input::get('first_name'),
                'last_name' => Input::get('last_name'),
                'street_1' => Input::get('street_1'),  
                'street_2' => Input::get('street_2'),  
                'apartment' => Input::get('apartment'),
                'phone_1' => Input::get('phone_1'),  
                'phone_2' => Input::get('phone_2'),
                'state' => 'Alabama',
              'user_zip_code' => Input::get('user_zip_code'),
              'city' => $zip_code->city
            ];                
            $rules = array(
                'first_name' => 'required',
                'last_name' => 'required',
                'street_1' => 'required',
                'phone_1' => 'required'
            );

            $validation = Validator::make($userdata, $rules);

            if($validation->fails()){
                return Redirect::to("customer/edit/$id")->withErrors($validation)->withInput();
            } 

            UsersInformation::find($id)->update($userdata);

            return Redirect::back();

Blade template

    {{ Form::open(array('name' => 'edit customer', 'action' => array('CustomerController@saveCustomer', $customer->id)))}}
       @foreach($errors->all() as $error)
                <p class="albox errorbox">{{$error}}</p>
            @endforeach
            <div class="four columns">      
        @if (isset($customer->first_name))
            {{ Form::hidden('user_id', '' . $customer->user_id . '')}}
        @endif                  
        <label class="form">First Name:</label> <span class="required"> (required)</span><br />
        @if (isset($customer->first_name))
            {{ Form::text('first_name', '' . $customer->first_name . '')}}<br />
        @else
            {{ Form::text('first_name')}}<br />
        @endif

        <label class="form">Last Name:</label> <span class="required"> (required)</span><br />
        @if (isset($customer->last_name))
            {{ Form::text('last_name', '' . $customer->last_name . '')}}<br />
        @else
            {{ Form::text('last_name')}}<br />
        @endif

        <label class="form">Primary Street:</label> <br />
        @if (isset($customer->street_1))
            {{ Form::text('street_1', '' . $customer->street_1 . '')}}<br />
        @else
            {{ Form::text('street_1')}}<br />
        @endif  

        <label class="form">Secondary Street:</label> <br />
        @if (isset($customer->street_2))
            {{ Form::text('street_2', '' . $customer->street_2 . '')}}<br />
        @else
            {{ Form::text('street_2')}}<br />
        @endif              

        <label class="form"> Primary Phone Number:</label> <br />
        @if (isset($customer->phone_1))
            {{ Form::text('phone_1', '' . $customer->phone_1 . '')}}<br />
        @else
            {{ Form::text('phone_1')}}<br />
        @endif

        <label class="form">Secondary Phone Number:</label> <br />
        @if (isset($customer->phone_2))
            {{ Form::text('phone_2', '' . $customer->phone_2 . '')}}<br />
        @else
            {{ Form::text('phone_2')}}<br />
        @endif          

        <label class="form">Apartment:</label><br />
        @if (isset($customer->apartment))
            {{ Form::text('apartment', '' . $customer->apartment . '')}}<br />
        @else
            {{ Form::text('apartment')}}<br />
        @endif
<label class="form">City/Zip Code:</label> <span class="required"> (required)</span><br />      
        <select name="user_zip_code"> 
            <option value='{{{ $customer->user_zip_code }}}'>{{{ $customer->city  }}} - {{{ $customer->user_zip_code }}}</option>
             @foreach ($zip_code as $zip_codes)
                <option value='{{ $zip_codes->zip_code  }}'>{{ $zip_codes->city  }} - {{ $zip_codes->zip_code  }}</option>
             @endforeach
        </select><br />         

<button type="submit" class="button is-success">Save</button><br /><br />
            </div>
{{ Form::close() }}

Not sure if needed but here is also my route.php page

Route::get('customer/edit/{id}', 'CustomerController@editCustomer');
Lynx
  • 1,462
  • 5
  • 32
  • 59

2 Answers2

0

By current page, do you mean the page where the form is? If yes, than it is in fact redirecting back – it goes to the controller action, process everything in the saveCustomer() and redirects back to the form page.

The question is...is it saving the user in the DB?

P.S You should have all you database logic in the model rather than in the controller – makes the whole app more modular, easier to test and your controllers thinner.

Edit Use a named route, and redirect explicitly to it.

Routes.php

Route::get('/customer', array('as' => 'customer', 'uses' => 'MyController@showCustomer'));

Controller.php

function saveCustomer($id) {

    ...

    Redirect::route('customer');
}
Gabriel C. Troia
  • 3,180
  • 2
  • 16
  • 17
  • not the question is why is it re-directing to the same exact page rather than the previous page the user was on. – Lynx Dec 10 '13 at 03:15
  • If I understand it well: from page A you go to page B(the form page), submit the form and expect to return to page A? if this is the case, you are missing page C(the controller page) for which back is page B, not page A. Use intended, or a specific named route for this. – Gabriel C. Troia Dec 10 '13 at 03:19
  • It does have the controller. Route::get('customer/edit/{id}', 'CustomerController@editCustomer'); ..so Basically when I am at the root (/) and go to (customers) and save the form. It will run through the controller for that route editCustomer and then return back to (customers) I need it to go to (/) – Lynx Dec 10 '13 at 03:37
  • 1
    It's how it's suppose to be. The controller method, saveCustomer(), is a separate page, exactly like the '/' or '/customers'. To try it out – set a return at the beginning of saveCustomer() and see it for yourself. – Gabriel C. Troia Dec 10 '13 at 03:41
  • Setting it at the beginning of the Controller does the same exact thing. It goes to the fall-back page – Lynx Dec 10 '13 at 03:48
  • did you try to set a named route for your route, and redirect to that from the Controller? See the edited answer – Gabriel C. Troia Dec 10 '13 at 03:52
0

Use:

if(isset($_SERVER['HTTP_REFERER']))
    return Redirect::back();

return Redirect::to('/');