如何在laravel中的参数中传递带有$ request的if语句

时间:2017-12-27 08:16:00

标签: php laravel

我在这里真的很棒。我有一个网页,可以在表格中显示人物的姓名。在这张表中有一个选择形式,我指出它们是否是一个好面条"。在数据库中,好的noodle变量是一个布尔值。我将把我的代码放在下面。我的问题是,我不能让它更新我在好面条选项中选择的任何内容以重新存在兄弟的记录。

当我尝试打印并查看其价值时,它的工作正常。但无论出于何种原因,它都不会更新记录。

我的控制器:

public function update(Request $request)
{
    $brothers = Brother::all();

    foreach ($brothers as $b){
        $b->GoodNoodle = $request['goodNoodle'];
        $b->save();

        //Failed attempt #1
        //if ($request['goodNoodle'] == "No"){
            //$b->GoodNoodle = "0";
            //$b->save();
            //}
        //else{
            //$b->GoodNoodle = "1";
            //$b->save();
            //}

        //Failed Attempt #2 ive also tried every variation of this
        //if($b['goodNoodle'] != $request['goodNoodle']){
          //$b->GoodNoodle = $request->goodNoodle;
          //$b->save();
        //}
    }

}

我的blade.php:

<form method="Post" action="{{route('update_brother')}}">
            <input type="hidden" name="_token" value="{{csrf_token()}}">
            <table class="table">
                <tbody class="text-center">
                <tr>
                    <th>Good Noodle</th>
                    <th>Brother Name</th>
                    <th>Brother Class</th>
                    <th>Zeta Number</th>
                </tr>
                @foreach($brothers as $b)
                    <tr>
                        <td>
                            <select class="form-control" id="exampleFormControlSelect1" name="goodNoodle">
                                <option value="1">Yes</option>
                                <option value="0"{{!$b["goodNoodle"]? "selected": ""}}>No</option>
                            </select>
                        </td>
                        <td>
                            {{$b->fName.' '.$b->lName}}
                        </td>
                        <td>
                            {{$b->zetaClass}}
                        </td>
                        <td>
                            <input type="hidden" name="zetaNum">{{$b->zetaNum}}
                        </td>
                    </tr>
                @endforeach
                </tbody>
            </table>
            <input class="btn btn-primary btn-block" type="submit" value="Submit">
        </form>

以下是一些线索。如果我这样做:

$value = $request->GoodNoodle; //with $value being equal to 1

if $b['goodNoodle'] = $value{
   $b->goodNoodle = "0"
} 

然后它会将我的所有记录更改为0.但如果我这样做

$value = $request->GoodNoodle; //with $value being equal to 1

if $b['goodNoodle'] = $value{
   $b->goodNoodle = $value
} 

它不会做任何事情。请帮忙。

3 个答案:

答案 0 :(得分:1)

您也可以使用三元运算符。

<select class="form-control" id="exampleFormControlSelect1" name="goodNoodle">
    <option value="0" {{ $b->goodNoodle == '0' ? 'selected' : '' }}>No</option>
    <option value="1" {{ $b->goodNoodle == '1' ? 'selected' : '' }}>Yes</option>
</select>

答案 1 :(得分:0)

如果要比较DB和请求中的值,请执行以下操作:

def get(self, timeout):
    """
    :param timeout: How many seconds to wait
    :type timeout: int
    :rtype: threading.Event
    """
    # tricks here
    return self._latest

而不是:

if ($b['goodNoodle'] == $request->goodNoodle)

答案 2 :(得分:0)

您应该更改以下选择框:

<select class="form-control" id="exampleFormControlSelect1" name="goodNoodle">
 <option value="0" @if($b->goodNoodle == '0') selected @endif>No</option>
 <option value="1" @if($b->goodNoodle == '1') selected @endif>Yes</option>
</select>