1

I've tried number of solutions from other posts but still can't get it work.

I have two forms on the page(view)

{{ Form::open(array('action' => 'AdminController@shopMode')) }}
   ....// form fields

   <button type="submit" class="btn btn-primary">Change</button>
{{ Form::close() }}           
    <hr/> 
{{ Form::open(array('action' => 'AdminController@preferencesSubmit')) }}
   ....// second form fields

   <button type="submit" class="btn btn-primary">Save Changes</button>
{{ Form::close() }}

Then in routes I have

Route::post('/admin/preferences', ['uses' => 'AdminController@preferencesSubmit', 'before' => 'csrf|admin']);
Route::post('/admin/preferences', ['uses' => 'AdminController@shopMode', 'before' => 'csrf|admin']);

When I hit submit button nothing change in database. Just page is refreshed and I got success message from FIRST form even if I submit second one.

Is it because url's in routes are same for both posts?

Update: First form input field:

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" <?php if ($settings['preferences_shop_mode'] == 0){ ?> checked="checked" value="1"<?php }else{ ?> value="0" <?php } ?>>

Here I check if preference is =0 to set value to 1 otherwise value = 0. In source I see that value is =1 which is correct because in database I have 0

<input type="checkbox" name="onoffswitch" class="onoffswitch-checkbox" id="myonoffswitch" checked="checked" value="1">

This is the controller

public function shopMode() {

    $preferences = Preferences::where('preferences_id', 1)->first();
    if (!$preferences) {
        App::abort(404);
    }

    Input::merge(array_map('trim', Input::all()));

    $preferences->preferences_shop_mode = Input::get('onoffswitch');          
    $preferences->save();
    return Redirect::to('/admin/preferences')->with('message', 'Shop mode changed successfully.');
} 

Any idea why isn't updated in database?

Jason Paddle
  • 1,095
  • 1
  • 19
  • 37

1 Answers1

2

Routes are read in cascade. Since both routes have the same path, the first takes priority (an entry was found, so no further route lookup is needed).

You should split them with just different paths, for example:

Route::post('/admin/preferences/general', ['uses' => 'AdminController@preferencesSubmit', 'before' => 'csrf|admin']);
Route::post('/admin/preferences/shop', ['uses' => 'AdminController@shopMode', 'before' => 'csrf|admin']);
alariva
  • 2,051
  • 1
  • 22
  • 37
  • 1
    Thank you very much but second form doesn't submit changes in database.. I have updated my question. Please can you check? – Jason Paddle Nov 29 '16 at 14:26
  • Well that's a totally different question. Checkboxes behavior are different from the rest, if it's unchecked, the field does not arrive on request. See [this question](http://stackoverflow.com/questions/1809494/post-the-checkboxes-that-are-unchecked) it will help. Let me know! – alariva Nov 29 '16 at 14:32
  • But I always have value either 0 or 1. When it's posted I should get this value, no? – Jason Paddle Nov 29 '16 at 14:33
  • Right now, when you post with checked control you are only getting `onoffswitch=1`, but when its unchecked you are not getting `onoffswitch=0`, right? – alariva Nov 29 '16 at 14:36
  • I get `` If I manually change in database to `0` I've got ``. I've got `value="1"` and `value="0"` depending of value in database. – Jason Paddle Nov 29 '16 at 14:37
  • If I understand well, its inverted? – alariva Nov 29 '16 at 14:40
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/129358/discussion-between-alariva-and-jason-paddle). – alariva Nov 29 '16 at 14:41
  • Yes, when in database is `0`on form is `value="1"` i.e. if I submit it should update database to `1`. And if in database is `1` on form will be `value="0"` and on submit is update it to 0 in db – Jason Paddle Nov 29 '16 at 14:41