php Laravel 10中的空请求参数

utugiqy6  于 11个月前  发布在  PHP
关注(0)|答案(2)|浏览(147)

我在Laravel 10中的项目有一个奇怪的问题。我的$request在我的控制器中是空的。

控制器

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

字符串

中间件

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 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>

路线

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


请求类是use Illuminate\Http\Request;我已经用dd($request)和Xdebug进行了双重检查,但都带有空参数。

6qfn3psc

6qfn3psc1#

这个问题可能是因为你在验证后重新分配了$request变量。validate方法只返回经过验证的数据,所以当你执行$request = $request->validate([...])时,你只使用经过验证的数据替换了整个请求对象。
尝试将您的代码更改为:

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

字符串
在这段代码中,经过验证的数据存储在一个单独的变量$validated中,原始的$request对象保持不变。这应该可以解决您的问题。

cuxqih21

cuxqih212#

为了避免重复原始的$request,请将验证过的数据分配给不同的变量。下面是更正后的代码:

$validatedData = $request->validate([
    'scroll_lock' => 'Boolean',
    'test_mode' => 'Boolean',
]);

字符串

相关问题