2015-09-21 31 views
0

试图改进我的应用程序的代码并刚刚迁移到L5。我曾经在我的视图中调用模型,我知道这不是最佳实践 - 我应该完全分离数据调用和视图。 但是你怎么处理这样的情况:在视图中调用模型的替代方法 - Laravel

  <div class="field"> 
      <label for="country">Country<sup>*</sup></label> 
      <select class="" id="country" name="country"> 
       <option value="{{{ old('country') ? old('country') : '' }}}">{{{ old('country') ? App\Country::find(old('country'))->country_name : '' }}}</option> 
       @foreach ($countries as $studio_country) 
       <option value="{{ e($studio_country->id) }}">{{ e($studio_country->country_name) }}</option> 
       @endforeach 
      </select> 
      @if ($errors->has('country')) 
      <div class="alert alert-warning" role="alert"> 
       {{ $errors->first('country') }} 
      </div> 
      @endif 
      <br> 
      </div> 

基本上,如果有一个输入和输入没有通过该页面与旧的输入和错误信息刷新验证。我需要从DB中提取国家的名称,因为我只有它的ID。

回答

0

你会想要做这样的事情:

<select id="country" name="country"> 
    @foreach ($countries as $studio_country) 
      <option 
       value="{{ $studio_country->id }}" 
       {{ $studio_country->id === old('country') ? 'selected' : '' }}> 
       {{ $studio_country->country_name }} 
      </option> 
    @endforeach 
</select> 
相关问题