<bdo id='3crDZ'></bdo><ul id='3crDZ'></ul>
  • <legend id='3crDZ'><style id='3crDZ'><dir id='3crDZ'><q id='3crDZ'></q></dir></style></legend>

        <small id='3crDZ'></small><noframes id='3crDZ'>

      1. <i id='3crDZ'><tr id='3crDZ'><dt id='3crDZ'><q id='3crDZ'><span id='3crDZ'><b id='3crDZ'><form id='3crDZ'><ins id='3crDZ'></ins><ul id='3crDZ'></ul><sub id='3crDZ'></sub></form><legend id='3crDZ'></legend><bdo id='3crDZ'><pre id='3crDZ'><center id='3crDZ'></center></pre></bdo></b><th id='3crDZ'></th></span></q></dt></tr></i><div id='3crDZ'><tfoot id='3crDZ'></tfoot><dl id='3crDZ'><fieldset id='3crDZ'></fieldset></dl></div>
        <tfoot id='3crDZ'></tfoot>

        Eloquent ->first() 如果 ->exists()

        时间:2023-09-23

          <small id='p7yPA'></small><noframes id='p7yPA'>

            <tbody id='p7yPA'></tbody>
          <i id='p7yPA'><tr id='p7yPA'><dt id='p7yPA'><q id='p7yPA'><span id='p7yPA'><b id='p7yPA'><form id='p7yPA'><ins id='p7yPA'></ins><ul id='p7yPA'></ul><sub id='p7yPA'></sub></form><legend id='p7yPA'></legend><bdo id='p7yPA'><pre id='p7yPA'><center id='p7yPA'></center></pre></bdo></b><th id='p7yPA'></th></span></q></dt></tr></i><div id='p7yPA'><tfoot id='p7yPA'></tfoot><dl id='p7yPA'><fieldset id='p7yPA'></fieldset></dl></div>
          <tfoot id='p7yPA'></tfoot>
            <legend id='p7yPA'><style id='p7yPA'><dir id='p7yPA'><q id='p7yPA'></q></dir></style></legend>

              <bdo id='p7yPA'></bdo><ul id='p7yPA'></ul>

                  本文介绍了Eloquent ->first() 如果 ->exists()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我想获取条件匹配的表中的第一行:

                  I want to get the first row in table where condition matches:

                  User::where('mobile', Input::get('mobile'))->first()
                  

                  效果很好,但如果条件不匹配,则会抛出异常:

                  It works well, but if the condition doesn't match, it throws an Exception:

                  ErrorException
                  Trying to get property of non-object
                  

                  目前我是这样解决的:

                  if (User::where('mobile', Input::get('mobile'))->exists()) {
                      $user = User::where('mobile', Input::get('mobile'))->first()
                  }
                  

                  我可以在不运行两个查询的情况下执行此操作吗?

                  Can I do this without running two queries?

                  推荐答案

                  注意:first() 方法不会像原始问题中描述的那样抛出异常.如果您收到此类异常,则说明您的代码中存在另一个错误.

                  使用 first() 并检查结果的正确方法:

                  The correct way to user first() and check for a result:

                  $user = User::where('mobile', Input::get('mobile'))->first(); // model or null
                  if (!$user) {
                     // Do stuff if it doesn't exist.
                  }
                  

                  其他技术(不推荐,不必要的开销):

                  Other techniques (not recommended, unnecessary overhead):

                  $user = User::where('mobile', Input::get('mobile'))->get();
                  
                  if (!$user->isEmpty()){
                      $firstUser = $user->first()
                  }
                  

                  try {
                      $user = User::where('mobile', Input::get('mobile'))->firstOrFail();
                      // Do stuff when user exists.
                  } catch (ErrorException $e) {
                      // Do stuff if it doesn't exist.
                  }
                  

                  // Use either one of the below. 
                  $users = User::where('mobile', Input::get('mobile'))->get(); //Collection
                  
                  if (count($users)){
                      // Use the collection, to get the first item use $users->first().
                      // Use the model if you used ->first();
                  }
                  

                  每种方法都是获得所需结果的不同方式.

                  Each one is a different way to get your required result.

                  这篇关于Eloquent ->first() 如果 ->exists()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:防止 Laravel 将多条记录添加到数据透视表 下一篇:Laravel 4 - 雄辩.无限的孩子​​变成可用的数组

                  相关文章

                  最新文章

                  <tfoot id='QzXrl'></tfoot>

                    1. <small id='QzXrl'></small><noframes id='QzXrl'>

                      <legend id='QzXrl'><style id='QzXrl'><dir id='QzXrl'><q id='QzXrl'></q></dir></style></legend>
                    2. <i id='QzXrl'><tr id='QzXrl'><dt id='QzXrl'><q id='QzXrl'><span id='QzXrl'><b id='QzXrl'><form id='QzXrl'><ins id='QzXrl'></ins><ul id='QzXrl'></ul><sub id='QzXrl'></sub></form><legend id='QzXrl'></legend><bdo id='QzXrl'><pre id='QzXrl'><center id='QzXrl'></center></pre></bdo></b><th id='QzXrl'></th></span></q></dt></tr></i><div id='QzXrl'><tfoot id='QzXrl'></tfoot><dl id='QzXrl'><fieldset id='QzXrl'></fieldset></dl></div>
                        <bdo id='QzXrl'></bdo><ul id='QzXrl'></ul>