它可能只有一个foreach在这个代码而不是两个?

时间:2018-03-14 12:34:45

标签: php laravel laravel-5 laravel-5.5 laravel-blade

我有一个页面来编辑会议的每种注册类型的表单。在此表单中,有一个表格,显示该会议存在的问题,然后用户可以选择在哪些注册类型中包含该特定问题的复选框。

始终包含字段名称和姓氏,因此该字段没有复选框。但是对于其他问题,用户可以选择在哪个注册类型中包含每个问题。

代码正常运行,问题是我在同一页面上有以下代码2次:

@foreach($registration_type as $rtype)
   ...
@endforeach

您知道是否可以更改代码,以便在访问此页面时没有太多请求?

查看

...
<tr>    
    <td>Email</td>
    <td>
        @foreach($registration_type as $rtype)
        <div class="form-check">
            <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
            <label class="form-check-label" for="exampleRadios1"> {{$rtype->name}}
            </label>
        </div>
        @endforeach
    </td>
    <td>
        @foreach($registration_type as $rtype)
        <div class="form-check">
            <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
            <label class="form-check-label" for="exampleRadios1"> for the registration type "{{$rtype->name}}"
            </label>
        </div>
        @endforeach
    </td>
</tr>

@foreach($question as $q)
<tr>
    <td>{{$q->question}}</td>
    <td>
        @foreach($registration_type as $rtype)
        <div class="form-check">
            <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
            <label class="form-check-label" for="exampleRadios1"> {{$rtype->name}}
            </label>
        </div>
        @endforeach
    </td>
    <td>
        @foreach($registration_type as $rtype)
        <div class="form-check">
            <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $rtype->id }}" id="{{$rtype->id}}">
            <label class="form-check-label" for="exampleRadios1">for the registration type "{{$rtype->name}}"
            </label>
        </div>
        @endforeach
    </td>
</tr>
@endforeach
...

控制器

class QuestionController extends Controller 
{ 
    public function edit($id) 
    { 
        $conf = Conf::find($id); 
        $registrationType = RegistrationType::where('conf_id', $id)->get(); 
        $question = Question::where('conf_id', $id)->get();

        return view('questions.edit')->with('conf', $conf)->with('registration_type', $registrationType)->with('question', $question);
    }
}

1 个答案:

答案 0 :(得分:0)

如果您受到代码欺骗性的困扰,您可以&#34; export&#34;代码到自己的视图并传递@include('view', ['param' => 'value'])

中的参数

<强>登记-type.blade.php

@foreach($types as $type)
    <div class="form-check">
        <input autocomplete="off" class="form-check-input" type="checkbox" value="{{ $type->id }}" id="{{ $type->id }}">
        <label class="form-check-label" for="exampleRadios1">for the registration type "{{ $type->name }}"
        </label>
    </div>
@endforeach

并在您的视图中使用

@include('registration-type', ['types' => $registration_type])

当然,现在你将有两个@include语句,所以不会处理双重性,但它看起来更好。