2015-09-21 33 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> 
相關問題