在Laravel HTML Helper输入中打印变量

时间:2016-03-12 17:48:41

标签: twitter-bootstrap laravel-5 laravel-blade

我有一个使用Laravel Form Helper创建的Laravel表单。我希望每个字段都是唯一的,所以我声明了一个计数器变量:

{{counter=0}}

我在foreach循环中增加了这个变量,所以我可以在每个输入的ID元素上使用它,如下所示:

{{Form::text('company_lookup', null, ['class'=>'form-control typeahead', 'id'=>'company_lookup_{{$counter}}', 'placeholder' => 'Type company registration number or name'])}}   

执行此操作的正确语法是什么?

更新

@if(count(Auth::user()->institution_user))
{{$counter=0}}
@foreach(Auth::user()->institution_user as $company)
{{$counter++}}
<input type='hidden' value="{{$counter}}" name="lookup"/>
<div class="col-md-4">
    <div class="form-group">
        {{Form::label('company_lookup', 'Institution lookup', array('class' => 'control-label'))}}
        {{Form::text('company_lookup', null, ['class'=>'form-control typeahead', 'id'=>'company_lookup_{{$counter}}',
        'placeholder' => 'Type company registration number or name', 'lookup'=>'$counter'])}}
    </div>
</div>

<div class="col-md-4 mr-5">
    <div class="form-group">
        {{Form::label('company_registration_number', 'Registration Number', array('class' => 'control-label'))}}
        {{Form::text('company_registration_number[]', $company->company_registration_number, ['class'=>'form-control',
        'id'=>'company_registration_number_$counter'])}}
    </div>
</div>
<div class="col-md-7">
    <div class="form-group">
        {{Form::label('company_name', 'Institution Name', array('class' => 'control-label'))}}
        {{Form::text('company_name[]', $company->company_name, ['class'=>'form-control',
        'id'=>'company_name_$counter'])}}
    </div>
</div>
<div class="col-md-11">
    <div class="form-group">
        {{Form::label('company_address', 'Business Address', array('class' => 'control-label'))}}
        {{Form::textarea('company_address[]', $company->company_address, array('class' => 'form-control', 'rows' => '3',
        'id'=> 'company_address_$counter'))}}

    </div>
</div>
<div class="col-md-11">
    <hr>
</div>
@endforeach

1 个答案:

答案 0 :(得分:0)

您可以获得这样的枚举计数器:

@foreach(Auth::user()->institution_user as $index => $company)

$index在刀片内部可用,并且会在每次迭代中递增。

此外,如果您想在示例中使用$counter,您只需使用<?php ?>代替{{ }}