是否可以将变量分配给默认值以在Laravel中选择表单 [英] Is it possible to assign variable to default value to select form in Laravel

查看:115
本文介绍了是否可以将变量分配给默认值以在Laravel中选择表单的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我的问题是,我有一个表单,我希望这个表单具有默认值,这就是为什么我分配了$ s变量。

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未定义变量:s(查看:C:\xampp\这个变量来自Controller,但它总是给出错误; htdocs\X-GOTL\resources\views\actions\activity.blade.php)

我知道这个不能工作,因为在开始没有默认值,因为它没有被控制器发送。我尝试了一些ifs来解决它,但反正它不工作。此$ s值也等于此形式中选定值的数量。任何想法如何解决这个问题?

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)!!}

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

    }


推荐答案

答案很简单,只要添加withInput()

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


返回重定向('/ activity') - > with('stat', 'xxx') - > withInput();

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

这篇关于是否可以将变量分配给默认值以在Laravel中选择表单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆