<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>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                在 Laravel 5.4 中,新增了一個方法 withDefault,它是結合 belongsTo 一起使用的。所以先要知道,什么是 belongsTo 關聯? 什么是 belongsTo 關聯 belongsTo 關聯是一對一、一對多關系中,在定義反向關聯關系(Inverse Of The Relationship)的 Model 中使用的。比如: 一對一關系:一個 User 對應一部 Phone,在 Phone Model 中定義的關聯就是「一對一關系的反向關聯」。 ~~~ <?php namespace App; use App\User; use Illuminate\Database\Eloquent\Model; class Phone extends Model { /** * Get the user that owns the phone. */ public function user() { return $this->belongsTo(User::class); } } ~~~ 一對多關系:一個 User 對應多篇 Article,在 Article Model 中定義的關聯就是「一對多關系的反向關聯」。 ~~~ <?php namespace App\Models; use App\User; use Illuminate\Database\Eloquent\Model; class Article extends Model { /** * Get the author that owns the article. */ public function user() { return $this->belongsTo(User::class); } } ~~~ withDefault 為什么要有 這個問題問得好 在實際使用中,可能會出現這樣的情況:一個用戶刪除了,但是與這個用戶對應的 Phone 和 Article 記錄沒有刪除。當我們通過 Phone Model 和 Article Model 查詢用戶時,就有可能返回一個 null 值。 有些時候這個 null 值會引發一些問題。為了解決這個問題, withDefault 應運而生。 ~~~ /** * Get the author that owns the article. */ public function user() { return $this->belongsTo(User::class)->withDefault(); } ~~~ 這種情況下,withDefault 會返回一個 User Model 實例,從而避免了 null 引發的問題。當然,你還可以為這個實例填充數據:使用數組或者閉包形式的參數。 ~~~ /** * Get the author of the article. */ public function user() { return $this->belongsTo(User::class)->withDefault([ 'name' => '佚名', ]); } ~~~ ~~~ /** * Get the author of the article. */ public function user() { return $this->belongsTo(User::class)->withDefault(function ($user) { $user->name = '佚名'; }); } ~~~
                  <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>

                              哎呀哎呀视频在线观看