skip to Main Content

I have a strange problem with my project in Laravel 10. My $request comes empty in my controller.

Controller

public function postSettings(Request $request){
    $user = $request->user();
    $request = $request->validate([
        'scroll_lock' => 'boolean',
        'test_mode' => 'boolean',
    ]);
    $settings =  Settings::find($user->id) ? Settings::where('user_id', $user->id)->first() :  new Settings();
    $settings->scroll_lock = $request['scroll_lock'];
    $settings->test_mode = $request['test_mode'];
    $settings->save();
    return route('get-settings');
}

Middleware

public function handle(Request $request, Closure $next): Response
{

    $user = Auth::user();
    if( $user->user_type == UserType::Manager || $user->user_type == UserType::Operator ) {
        return redirect(RouteServiceProvider::QUESTIONS);
    }
    else {
        return $next( $request );
    }

Form

    <form method="post" action="{{ route('post-settings') }}">
        @csrf
        <div id=""  class="my-4">
            <div class="activity card" style="--delay: .2s">
                <div class="row">
                    <h2 class="question-title text-justify">Settings</h2>
                </div>
                <div class="answers-container h-100">
                    <div class="align-content-center">
                        <div class="m-3 row">
                            <div class="col-6">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" name="scroll-lock" role="switch" id="flexSwitchCheckChecked" @if($dataview->settings->scroll_lock)value="1" checked @else value="0" @endif>
                                    <label class="form-check-label" for="flexSwitchCheckChecked">Disabilita blocco scroll nel questionario.</label>
                                </div>
                            </div>
                            <div class="col-6">
                                <div class="form-check form-switch">
                                    <input class="form-check-input" type="checkbox" name="test-mode" role="switch" id="flexSwitchCheckChecked" @if($dataview->settings->test_mode)value="1" checked @else value="0" @endif>
                                    <label class="form-check-label" for="flexSwitchCheckChecked">Abilita la modalità di test</label>
                                    <p>Invio email disabilitato, questionario ridotto.</p>
                                </div>
                            </div>
                        </div>
                        <div class="my-3 row">
                            <div class="col-2">
                                <button type="submit" class="btn btn-primary btn-md next">Save</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </form>

Routes

Route::get('/settings', [DashboardController::class, 'getSettings'] )->middleware(['auth', 'verified', 'switchUserType'])->name('get-settings');

Route::post('/settings', [DashboardController::class, 'postSettings'] )->middleware(['auth', 'verified', 'switchUserType'])->name('post-settings');

The request class is use IlluminateHttpRequest; I already have double-checked with dd($request) and Xdebug, but both come with empty parameters. Any ideas?

2

Answers


  1. The issue might be due to the fact that you’re reassigning the $request variable after validation. The validate method returns only the validated data, so when you do $request = $request->validate([...]), you’re replacing the entire request object with just the validated data.

    Try changing your code to this:

    public function postSettings(Request $request){
        $user = $request->user();
        $validated = $request->validate([
            'scroll_lock' => 'boolean',
            'test_mode' => 'boolean',
        ]);
        $settings =  Settings::find($user->id) ? Settings::where('user_id', $user->id)->first() :  new Settings();
        $settings->scroll_lock = $validated['scroll_lock'];
        $settings->test_mode = $validated['test_mode'];
        $settings->save();
    
        return route('get-settings');
    }
    

    In this code, the validated data is stored in a separate variable, $validated, and the original $request object remains intact. This should solve your problem.

    Login or Signup to reply.
  2. To avoid overwriting the original $request, assign the validated data to a different variable. Here’s the corrected code:

    $validatedData = $request->validate([
        'scroll_lock' => 'Boolean',
        'test_mode' => 'Boolean',
    ]);
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search