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

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

        如何从原始对象创建 Eloquent 模型实例?

        时间:2023-09-24

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

                1. <i id='qbF5D'><tr id='qbF5D'><dt id='qbF5D'><q id='qbF5D'><span id='qbF5D'><b id='qbF5D'><form id='qbF5D'><ins id='qbF5D'></ins><ul id='qbF5D'></ul><sub id='qbF5D'></sub></form><legend id='qbF5D'></legend><bdo id='qbF5D'><pre id='qbF5D'><center id='qbF5D'></center></pre></bdo></b><th id='qbF5D'></th></span></q></dt></tr></i><div id='qbF5D'><tfoot id='qbF5D'></tfoot><dl id='qbF5D'><fieldset id='qbF5D'></fieldset></dl></div>
                    <tbody id='qbF5D'></tbody>
                2. <small id='qbF5D'></small><noframes id='qbF5D'>

                3. <tfoot id='qbF5D'></tfoot>
                  本文介绍了如何从原始对象创建 Eloquent 模型实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我需要使用 Laravel 进行原始数据库查询:

                  I need to make a raw database query using Laravel:

                  $results = DB::select("SELECT * FROM members 
                      INNER JOIN (several other tables) 
                      WHERE (horribly complicated thing) 
                      LIMIT 1");
                  

                  我得到一个普通的 PHP StdClass 对象,其中包含成员表上的属性字段.我想将其转换为 Member(一个 Eloquent 模型实例),如下所示:

                  I get back a plain PHP StdClass Object with fields for the properties on the members table. I'd like to convert that to a Member (an Eloquent model instance), which looks like this:

                  use IlluminateDatabaseEloquentModel;
                  
                  class Member extends Model {
                  }
                  

                  我不知道该怎么做,因为会员没有设置任何字段,而且我担心我不会正确初始化它.实现这一目标的最佳方法是什么?

                  I'm not sure how to do it since a Member doesn't have any fields set on it, and I'm worried I will not initialize it properly. What is the best way to achieve that?

                  推荐答案

                  您可以尝试将结果混合到模型对象中:

                  You can try to hydrate your results to Model objects:

                  $results = DB::select("SELECT * FROM members 
                                         INNER JOIN (several other tables) 
                                         WHERE (horribly complicated thing) 
                                         LIMIT 1");
                  
                  $models = Member::hydrate( $results->toArray() );
                  

                  或者你甚至可以让 Laravel 从原始查询中为你自动补水:

                  Or you can even let Laravel auto-hydrate them for you from the raw query:

                  $models = Member::hydrateRaw( "SELECT * FROM members...");
                  

                  编辑

                  从 Laravel 5.4 hydrRaw 开始不再可用.我们可以使用 fromQuery 代替:

                  From Laravel 5.4 hydrateRaw is no more available. We can use fromQuery instead:

                  $models = Member::fromQuery( "SELECT * FROM members..."); 
                  

                  这篇关于如何从原始对象创建 Eloquent 模型实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:获取 Eloquent 模型的关系数组 下一篇:带有一些约束的 Eloquent 嵌套关系

                  相关文章

                  最新文章

                    <small id='8hO95'></small><noframes id='8hO95'>

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

                    2. <legend id='8hO95'><style id='8hO95'><dir id='8hO95'><q id='8hO95'></q></dir></style></legend>
                        <bdo id='8hO95'></bdo><ul id='8hO95'></ul>
                      <tfoot id='8hO95'></tfoot>