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

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                在 Laravel 新版本當中,有一個非常有用的輔助方法: optional() 這個方法的應用場景是什么呢?其實如果你多寫代碼的話,經常會遇到類似下面的報錯信息: ~~~ Call to a member function on null object ... ~~~ 這其實是在我們的代碼中存在著某個對象為空,所以在我們調用方法或者獲取屬性的時候就會報出以上錯誤信息。比如我們舉個例子: 在 User 的體系中,我們也有 Profile 的 Model 文件,而一個 User 擁有一個 Profile ( Profile 中包含用戶的地址信息) ~~~ public function profile(){ return $this->hasOne(Profile::class); } ~~~ 然后在我們 User 的信息頁面,我們希望通過下面的代碼獲取 User 的地址: ~~~ $user->profile->address; ~~~ 如果在數據庫中,我們沒有該 User 對應的 Profile,這個時候就會出現文章開始說到的類似錯誤了。 所以,這個時候, optional() 就可以派上用場了 我們只需要這樣獲取用戶的地址信息就好: ~~~ $user->optional(profile)->address ~~~ 這個時候,即使 profile 為空值( null),這一行代碼也不會報錯,而是會顯示一個空的字符串出來。 是不是非常有用!有了 optional() 這個輔助函數之后,在很多類似的代碼中,在你不確定是否該對象會為 null 的時候,就可以加上 optional() 來做保險了!
                  <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>

                              哎呀哎呀视频在线观看