Is it possible to assign variable to default value

2019-08-25 01:10发布

So my question is, I have a form and I want this form to have default value and thats why I assigned $s variable. This variable comes from Controller, but It always gives error ;

(2/2) ErrorException Undefined variable: s (View: C:\xampp\htdocs\X-GOTL\resources\views\actions\activity.blade.php)

I know that this cant work because in the beginning there is no default value because its not sent by controller yet. I tried some ifs to fix it but anyways its not working. Also this $s value is equals to number of selected value in this form. Any ideas how I can fix this problem?

{!! Form::select('number', ['1' => 'Koşuya Çıkmak', '2' => 'Yoga Yapmak'], $s) !!}

This is code in controller.

    $activity = $request->number;

    $user_stat = Auth::user()->stat;
    $job_user = Auth::user()->stat;


    $userInfo = Auth::user();
    $id = $userInfo->id;
    $user = User::find($userInfo->id);


    if($activity == 1){

        $s = $request->number;


        $user_stat->str = Auth::user()->stat->str + 4;
        $user_stat->dex = Auth::user()->stat->dex + 4;

        $job_user->save();

        return redirect('/activity')->with('stat', '4 Güç ve 4 Beceri Kazandın!')->with(compact('s'));

    }
    elseif($activity == 2){

        $s = $request->number;

        $user_stat->str = Auth::user()->stat->str + 4;
        $user_stat->int = Auth::user()->stat->int + 4;

        $job_user->save();

        return redirect('/activity')->with('stat', '4 Güç ve 4 Zeka Kazandın!')
            ->with(compact('s'));

    }

1条回答
孤傲高冷的网名
2楼-- · 2019-08-25 01:43

The answer was so easy guys.. Just to add withInput()

return redirect('/activity')->with('stat', 'xxx')->withInput();

查看更多
登录 后发表回答