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

    <tfoot id='rAuZr'></tfoot>

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

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

    2. 获取 Eloquent 模型的关系数组

      时间:2023-09-24

              <tbody id='uV0N0'></tbody>

          • <small id='uV0N0'></small><noframes id='uV0N0'>

            <legend id='uV0N0'><style id='uV0N0'><dir id='uV0N0'><q id='uV0N0'></q></dir></style></legend>
            <tfoot id='uV0N0'></tfoot>

            <i id='uV0N0'><tr id='uV0N0'><dt id='uV0N0'><q id='uV0N0'><span id='uV0N0'><b id='uV0N0'><form id='uV0N0'><ins id='uV0N0'></ins><ul id='uV0N0'></ul><sub id='uV0N0'></sub></form><legend id='uV0N0'></legend><bdo id='uV0N0'><pre id='uV0N0'><center id='uV0N0'></center></pre></bdo></b><th id='uV0N0'></th></span></q></dt></tr></i><div id='uV0N0'><tfoot id='uV0N0'></tfoot><dl id='uV0N0'><fieldset id='uV0N0'></fieldset></dl></div>
                <bdo id='uV0N0'></bdo><ul id='uV0N0'></ul>
              • 本文介绍了获取 Eloquent 模型的关系数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在尝试获取所有模型关联的数组.我有以下模型:

                I'm trying to get an array of all of my model's associations. I have the following model:

                class Article extends Eloquent 
                {
                    protected $guarded = array();
                
                    public static $rules = array();
                
                    public function author() 
                    {
                        return $this->belongsTo('Author');
                    }
                
                    public function category() 
                    {
                        return $this->belongsTo('Category');
                    }
                }
                

                从这个模型中,我试图获得以下关系数组:

                From this model, I'm trying to get the following array of its relations:

                array(
                    'author',
                    'category'
                )
                

                我正在寻找一种方法来自动从模型中拉出这个数组.

                I'm looking for a way to pull this array out from the model automatically.

                我找到了这个定义Eloquent 模型上的relationsToArray 方法,它似乎返回模型关系的数组.它似乎使用了 Eloquent 模型的 $this->relations 属性.然而,这个方法返回一个空数组,并且关系属性是一个空数组,尽管我的关系设置正确.

                I've found this definition of a relationsToArray method on an Eloquent model, which appears to return an array of the model's relations. It seems to use the $this->relations attribute of the Eloquent model. However, this method returns an empty array, and the relations attribute is an empty array, despite having my relations set up correctly.

                如果不存储模型关系,$this->relations 有什么用?有什么办法可以自动获取我的模型关系数组?

                What is $this->relations used for if not to store model relations? Is there any way that I can get an array of my model's relations automatically?

                推荐答案

                这是不可能的,因为只有在使用 with(用于急切加载)或使用定义的关系公共方法请求时才加载关系模型,例如,如果一个 Author 模型是用以下关系创建的

                It's not possible because relationships are loaded only when requested either by using with (for eager loading) or using relationship public method defined in the model, for example, if a Author model is created with following relationship

                public function articles() {
                    return $this->hasMany('Article');
                }
                

                当你像这样调用这个方法时:

                When you call this method like:

                $author = Author::find(1);
                $author->articles; // <-- this will load related article models as a collection
                

                另外,正如我所说的with,当你使用这样的东西时:

                Also, as I said with, when you use something like this:

                $article = Article::with('author')->get(1);
                

                在这种情况下,第一篇文章(id为1)将加载其相关模型Author,您可以使用

                In this case, the first article (with id 1) will be loaded with it's related model Author and you can use

                $article->author->name; // to access the name field from related/loaded author model
                

                因此,如果不使用适当的方法加载关系,就不可能神奇地获得关系,但是一旦加载了关系(相关模型),您就可以使用这样的方法来获取关系:

                So, it's not possible to get the relations magically without using appropriate method for loading of relationships but once you load the relationship (related models) then you may use something like this to get the relations:

                $article = Article::with(['category', 'author'])->first();
                $article->getRelations(); // get all the related models
                $article->getRelation('author'); // to get only related author model
                

                要将它们转换为 array,您可以使用 toArray() 方法,例如:

                To convert them to an array you may use toArray() method like:

                dd($article->getRelations()->toArray()); // dump and die as array
                

                relationsToArray() 方法适用于加载了相关模型的模型.该方法将相关模型转换为数组形式,其中toArray()方法将模型(有关系)的所有数据转换为数组,源代码如下:

                The relationsToArray() method works on a model which is loaded with it's related models. This method converts related models to array form where toArray() method converts all the data of a model (with relationship) to array, here is the source code:

                public function toArray()
                {
                     $attributes = $this->attributesToArray();
                
                     return array_merge($attributes, $this->relationsToArray());
                }
                

                将模型属性和与其相关的模型属性合并成数组后返回.

                It merges model attributes and it's related model's attributes after converting to array then returns it.

                这篇关于获取 Eloquent 模型的关系数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:Laravel Eloquent 和多重连接 下一篇:如何从原始对象创建 Eloquent 模型实例?

                相关文章

                最新文章

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

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

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