久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

    1. <small id='0Z08D'></small><noframes id='0Z08D'>

    2. <legend id='0Z08D'><style id='0Z08D'><dir id='0Z08D'><q id='0Z08D'></q></dir></style></legend>

        <bdo id='0Z08D'></bdo><ul id='0Z08D'></ul>

        <i id='0Z08D'><tr id='0Z08D'><dt id='0Z08D'><q id='0Z08D'><span id='0Z08D'><b id='0Z08D'><form id='0Z08D'><ins id='0Z08D'></ins><ul id='0Z08D'></ul><sub id='0Z08D'></sub></form><legend id='0Z08D'></legend><bdo id='0Z08D'><pre id='0Z08D'><center id='0Z08D'></center></pre></bdo></b><th id='0Z08D'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='0Z08D'><tfoot id='0Z08D'></tfoot><dl id='0Z08D'><fieldset id='0Z08D'></fieldset></dl></div>
        <tfoot id='0Z08D'></tfoot>

        使用 Eloquent 查找或創建

        Find or Create with Eloquent(使用 Eloquent 查找或創建)
        <tfoot id='4lUHk'></tfoot>

              <tbody id='4lUHk'></tbody>

            • <small id='4lUHk'></small><noframes id='4lUHk'>

                <legend id='4lUHk'><style id='4lUHk'><dir id='4lUHk'><q id='4lUHk'></q></dir></style></legend>

                  <bdo id='4lUHk'></bdo><ul id='4lUHk'></ul>
                • <i id='4lUHk'><tr id='4lUHk'><dt id='4lUHk'><q id='4lUHk'><span id='4lUHk'><b id='4lUHk'><form id='4lUHk'><ins id='4lUHk'></ins><ul id='4lUHk'></ul><sub id='4lUHk'></sub></form><legend id='4lUHk'></legend><bdo id='4lUHk'><pre id='4lUHk'><center id='4lUHk'></center></pre></bdo></b><th id='4lUHk'></th></span></q></dt></tr></i><div class="qwawimqqmiuu" id='4lUHk'><tfoot id='4lUHk'></tfoot><dl id='4lUHk'><fieldset id='4lUHk'></fieldset></dl></div>
                  本文介紹了使用 Eloquent 查找或創建的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

                  問題描述

                  我最近開始使用 Laravel 和 Eloquenta>,并且想知道缺少模型的查找或創建選項.你總是可以寫,例如:

                  I have recently started working with Laravel and Eloquent, and was wondering about the lack of a find or create option for models. You could always write, for example:

                  $user = User::find($id);
                  if (!$user) {
                      $user = new User;
                  }
                  

                  但是,沒有更好的方法來查找或創建嗎?在示例中這似乎微不足道,但對于更復雜的情況,獲取現有記錄并更新它或創建新記錄會非常有幫助.

                  However, is there not a better way to find or create? It seems trivial in the example, but for more complex situations it would be really helpfully to either get an existing record and update it or create a new one.

                  推薦答案

                  以下是被接受的原始答案:Laravel-4

                  Below is the original accepted answer for: Laravel-4

                  Laravel 中已經有一個方法 findOrFail 可用,當使用此方法時,它會在失敗時拋出 ModelNotFoundException 但在您的情況下,您可以通過在您的模型中創建一個方法來實現,例如,如果您有一個 User 模型,那么您只需將此函數放入模型中

                  There is already a method findOrFail available in Laravel and when this method is used it throws ModelNotFoundException on fail but in your case you can do it by creating a method in your model, for example, if you have a User model then you just put this function in the model

                  // Put this in any model and use
                  // Modelname::findOrCreate($id);
                  public static function findOrCreate($id)
                  {
                      $obj = static::find($id);
                      return $obj ?: new static;
                  }
                  

                  從您的控制器,您可以使用

                  From your controller, you can use

                  $user =  User::findOrCreate(5);
                  $user->first_name = 'John';
                  $user->last_name = 'Doe';
                  $user->save();
                  

                  如果存在 id5 的用戶,則更新該用戶,否則將創建一個新用戶,但 id將是 last_user_id + 1(自動遞增).

                  If a user with id of 5 exists, then it'll be updated, otherwise a new user will be created but the id will be last_user_id + 1 (auto incremented).

                  這是做同樣事情的另一種方式:

                  This is another way to do the same thing:

                  public function scopeFindOrCreate($query, $id)
                  {
                      $obj = $query->find($id);
                      return $obj ?: new static;
                  }
                  

                  你可以在Model中使用scope代替創建靜態方法,所以Model中的方法將是scopeMethodName和調用Model::methodName(),就像你在靜態方法中所做的一樣,例如

                  Instead of creating a static method, you can use a scope in the Model, so the method in the Model will be scopeMethodName and call Model::methodName(), same as you did in the static method, for example

                  $user =  User::findOrCreate(5);
                  

                  更新:

                  firstOrCreateLaravel 5x 中可用,答案太舊了,它在 2013 中為 Laravel-4.0 給出.

                  Update:

                  The firstOrCreate is available in Laravel 5x, the answer is too old and it was given for Laravel-4.0 in 2013.

                  在 Laravel 5.3 中,firstOrCreate 方法具有以下聲明:

                  In Laravel 5.3, the firstOrCreate method has the following declaration:

                  public function firstOrCreate(array $attributes, array $values = [])
                  

                  這意味著您可以像這樣使用它:

                  Which means you can use it like this:

                  User::firstOrCreate(['email' => $email], ['name' => $name]);
                  

                  僅通過電子郵件檢查用戶是否存在,但在創建時,新記錄將同時保存電子郵件和姓名.

                  User's existence will be only checked via email, but when created, the new record will save both email and name.

                  API 文檔

                  這篇關于使用 Eloquent 查找或創建的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

                  【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

                  相關文檔推薦

                  Deadlock exception code for PHP, MySQL PDOException?(PHP、MySQL PDOException 的死鎖異常代碼?)
                  PHP PDO MySQL scrollable cursor doesn#39;t work(PHP PDO MySQL 可滾動游標不起作用)
                  PHP PDO ODBC connection(PHP PDO ODBC 連接)
                  Using PDO::FETCH_CLASS with Magic Methods(使用 PDO::FETCH_CLASS 和魔術方法)
                  php pdo get only one value from mysql; value that equals to variable(php pdo 只從 mysql 獲取一個值;等于變量的值)
                  MSSQL PDO could not find driver(MSSQL PDO 找不到驅動程序)

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

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

                            主站蜘蛛池模板: 欧美成人一级视频 | 羞羞视频免费观 | 高清久久久 | 亚洲欧美在线观看 | 国产中文字幕亚洲 | 亚洲精品久久久久久久久久久久久 | 自拍偷拍亚洲视频 | 久久精品国产久精国产 | 午夜视频一区 | 国产高清一二三区 | 成人在线国产 | 欧美二区三区 | 亚洲午夜精品一区二区三区他趣 | 欧美888 | 日本污视频| 一区二区在线 | 欧美一区二区三区在线观看 | 中文字幕视频在线观看 | 欧美精品二区三区 | 亚洲 欧美 在线 一区 | 久久不射网 | 91九色在线观看 | 日韩免费视频一区二区 | 久久免费资源 | 欧美日韩成人 | 日日草天天干 | 久久精品视频12 | 国产高清在线精品 | 国产激情在线 | 91麻豆精品国产91久久久久久久久 | 免费观看黄a一级视频 | 国产欧美日韩精品一区二区三区 | 精品免费国产 | 精品成人 | 国产精品久久久久久久免费大片 | 午夜天堂精品久久久久 | 国产毛片久久久久久久久春天 | 国产精品久久久久婷婷二区次 | 国产精品国产精品国产专区不卡 | 国产精品视频网 | 婷婷色国产偷v国产偷v小说 |