<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??碼云GVP開源項目 12k star Uniapp+ElementUI 功能強大 支持多語言、二開方便! 廣告
                # 第6節 Laravel-通過模型處理性別 # 6.1 模型中創建處理性別的方法 6.2 模型方法在前臺視圖的遍歷、調用 ## 6.1 模型中創建處理性別的方法 ## 目前在列表頁的性別,顯示的還是數字。所以,我們有必要顯示成中文。 首先,在模型中創建一個 sex() 的方法。 `\app\models\Student.php` ~~~ <?php namespace App\models; use Illuminate\Database\Eloquent\Model; class Student extends Model { const SEX_UN = 2; const SEX_BOY = 1; const SEX_GIRL = 0; /** * 與模型相關的數據表 */ protected $table = "student"; /** * 指定是否模型應該被戳記時間 */ public $timestamps = false; /** * 處理用戶的性別,轉換為中文 * * @param $ind 用戶存儲的性別數字編號 * @return string 對應的性別中文字符 * @author webjust [604854119@qq.com] */ public function sex($ind = null) { $arr = array( self::SEX_GIRL => '女', self::SEX_BOY => '男', self::SEX_UN => '未知', ); if($ind !== null) { return array_key_exists($ind, $arr) ? $arr[$ind] : $arr[self::SEX_UN]; } return $arr; } } ~~~ 別忘了,既然要在模板中使用Student類,那就需要引入它。 在 `\resources\views\student\create.blade.php` 文件頂部加入如下代碼: <?php use App\Models\Student; $student = new Student(); ?> ## 6.2 模型方法在前臺視圖的遍歷、調用 ## 修改視圖 `\resources\views\student\create.blade.php` <div class="form-group"> <label class="col-sm-2 control-label">性別</label> <div class="col-sm-5"> @foreach($student->sex() as $ind => $sex) <label class="radio-inline"> <input type="radio" name="Student[sex]" {{ (isset(old('Student')['sex']) && old('Student')['sex'] == $ind) ? 'checked' : '' }} value="{{ $ind }}"> {{ $sex }} </label> @endforeach </div> <div class="col-sm-5"> <p class="form-control-static text-danger">{{ $errors->first('Student.sex') }}</p> </div> </div> 同理,我們在列表頁首頁也需要使用sex方法,因此也需要引入Student類。 在 `\resources\views\student\index.blade.php` 文件中,頂部寫入: <?php use App\Models\Student; $student = new Student(); ?> 然后在視圖文件中調用 sex() 方法,傳遞性別的值作為參數。 {{ $student->sex($student->sex) }} **顯示效果:** ![](https://box.kancloud.cn/1a6405873acbe7c8dd010e6540746046_1046x728.png)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看