视图Laravel中的预定义冗余值

时间:2019-02-21 05:42:37

标签: laravel laravel-5.7

我这里有一个编辑用户权限的简单模式。我想先显示所选用户的当前值,但似乎我的代码在重复时本质上是多余的。顺便说一下,这些值不在单独的表中。在此视图中,有没有更简单的方法?

<div class="form-group col-md-4">
    <label for="user_type">Privilege</label>
    <select name="user_type" class="form-control" id="">
        <?php $user_type = $user->user_type ?>
        @if ($user_type == 'Admin')
        <option value="Admin" selected>Admin</option>
        <option value="Facilitator">Facilitator</option>
        <option value="TeamLeader">TeamLeader</option>
        <option value="ScrumLeader">ScrumLeader</option>
        <option value="Member">Member</option>
        @elseif ($user_type == 'Facilitator')
        <option value="Admin">Admin</option>
        <option value="Facilitator" selected>Facilitator</option>
        <option value="TeamLeader">TeamLeader</option>
        <option value="ScrumLeader">ScrumLeader</option>
        <option value="Member">Member</option>
        @elseif ($user_type == 'TeamLeader')
        <option value="Admin">Admi   n</option>
        <option value="Facilitator">Facilitator</option>
        <option value="TeamLeader" selected>TeamLeader</option>
        <option value="ScrumLeader">ScrumLeader</option>
        <option value="Member">Member</option>
        @elseif ($user_type == 'ScrumLeader')
        <option value="Admin">Admin</option>
        <option value="Facilitator">Facilitator</option>
        <option value="TeamLeader">TeamLeader</option>
        <option value="ScrumLeader" selected>ScrumLeader</option>
        <option value="Member">Member</option>
        @else
        <option value="Admin">Admin</option>
        <option value="Facilitator">Facilitator</option>
        <option value="TeamLeader">TeamLeader</option>
        <option value="ScrumLeader">ScrumLeader</option>
        <option value="Member" selected>Member</option>
        @endif
    </select>
</div>

2 个答案:

答案 0 :(得分:1)

我建议使用 laravelcollective / html 软件包(here

然后,您将可以编写如下内容:

{{Form::select('user_type', $usertypearray, $user->user_type, ['class' => 'form-control']) }}

其中$ usertypearray是一个值=>标签数组。例如:

$usertypearray = ['admin'=> 'Admin', 'facilitator'=> 'Facilitator', 'teamleader'=> 'TeamLeader', 'scrumleader'=> 'ScrumLeader','member'=>'Member'];

(我将值小写,但检查它是否是服务器要查找的内容)

答案 1 :(得分:1)

您可以像这样在select标签上循环播放

  {{ Form::select('user_type',$user_type ,old('user_type'),['class' => 'form-control']) }}

希望这会有所帮助:)