pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

    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="nljn5xf" 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="lpn5h7r" 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="dxtrhjd" 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>

                            主站蜘蛛池模板: 自动化生产线-自动化装配线-直流电机自动化生产线-东莞市慧百自动化有限公司 | 水冷式工业冷水机组_风冷式工业冷水机_水冷螺杆冷冻机组-深圳市普威机械设备有限公司 | 小型单室真空包装机,食品单室真空包装机-百科 | 智能监控-安防监控-监控系统安装-弱电工程公司_成都万全电子 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 哔咔漫画网页版在线_下载入口访问指引| 砂石生产线_石料生产线设备_制砂生产线设备价格_生产厂家-河南中誉鼎力智能装备有限公司 | LZ-373测厚仪-华瑞VOC气体检测仪-个人有毒气体检测仪-厂家-深圳市深博瑞仪器仪表有限公司 | 列管冷凝器,刮板蒸发器,外盘管反应釜厂家-无锡曼旺化工设备有限公司 | 云南成人高考网| 液氮罐_液氮容器_自增压液氮罐-北京君方科仪科技发展有限公司 | 新能源汽车教学设备厂家报价[汽车教学设备运营18年]-恒信教具 | 江西自考网-江西自学考试网| 无锡装修装潢公司,口碑好的装饰装修公司-无锡索美装饰设计工程有限公司 | 滑石粉,滑石粉厂家,超细滑石粉-莱州圣凯滑石有限公司 | 滚筒线,链板线,总装线,流水线-上海体能机电有限公司 | 陶瓷加热器,履带式加热器-吴江市兴达电热设备厂 | 电子天平-华志电子天平厂家| 井式炉-台车式回火炉-丹阳市电炉厂有限公司 | 镀锌角钢_槽钢_扁钢_圆钢_方矩管厂家_镀锌花纹板-海邦钢铁(天津)有限公司 | 南京精锋制刀有限公司-纵剪机刀片_滚剪机刀片_合金刀片厂家 | 新疆乌鲁木齐网站建设-乌鲁木齐网站制作设计-新疆远璨网络 | 一礼通 (www.yilitong.com)-企业礼品解决方案一站式服务平台 | 冷却塔厂家_冷却塔维修_冷却塔改造_凉水塔配件填料公司- 广东康明节能空调有限公司 | WF2户外三防照明配电箱-BXD8050防爆防腐配电箱-浙江沃川防爆电气有限公司 | 继电器模组-IO端子台-plc连接线-省配线模组厂家-世麦德 | 【MBA备考网】-2024年工商管理硕士MBA院校/报考条件/培训/考试科目/提前面试/考试/学费-MBA备考网 | 中药二氧化硫测定仪,食品二氧化硫测定仪|俊腾百科 | 针焰试验仪,灼热丝试验仪,漏电起痕试验仪,水平垂直燃烧试验仪 - 苏州亚诺天下仪器有限公司 | 河南橡胶接头厂家,河南波纹补偿器厂家,河南可曲挠橡胶软连接,河南套筒补偿器厂家-河南正大阀门 | 全温度恒温培养摇床-大容量-立式-远红外二氧化碳培养箱|南荣百科 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 铆钉机|旋铆机|东莞旋铆机厂家|鸿佰专业生产气压/油压/自动铆钉机 | 液晶拼接屏厂家_拼接屏品牌_拼接屏价格_监控大屏—北京维康 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 光纤测温-荧光光纤测温系统-福州华光天锐光电科技有限公司 | 施工围挡-施工PVC围挡-工程围挡-深圳市旭东钢构技术开发有限公司 | 不锈钢钢格栅板_热浸锌钢格板_镀锌钢格栅板_钢格栅盖板-格美瑞 | 无硅导热垫片-碳纤维导热垫片-导热相变材料厂家-东莞市盛元新材料科技有限公司 | 小程序开发公司_APP开发多少钱_软件开发定制_微信小程序制作_客户销售管理软件-济南小溪畅流网络科技有限公司 | 分子精馏/精馏设备生产厂家-分子蒸馏工艺实验-新诺舜尧(天津)化工设备有限公司 |