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

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

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

        <tfoot id='kqAik'></tfoot>
      1. Laravel - Eloquent - 动态定义的关系

        时间:2023-09-24
      2. <small id='bdGzS'></small><noframes id='bdGzS'>

            <tbody id='bdGzS'></tbody>

            • <bdo id='bdGzS'></bdo><ul id='bdGzS'></ul>
            • <tfoot id='bdGzS'></tfoot>

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

                1. <legend id='bdGzS'><style id='bdGzS'><dir id='bdGzS'><q id='bdGzS'></q></dir></style></legend>

                  本文介绍了Laravel - Eloquent - 动态定义的关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  是否可以动态设置模型的关系?例如,我有模型 Page,我想在不实际更改其文件的情况下向其添加关系 banners()?那么是否存在这样的事情:

                  Is it possible to set a model's relationship dynamically? For example, I have model Page, and I want to add relationship banners() to it without actually changing its file? So does something like this exist:

                  Page::createRelationship('banners', function(){
                      $this->hasMany('banners');
                  });
                  

                  或者类似的东西?由于它们无论如何都是使用魔术方法获取的,也许我可以动态添加关系?

                  Or something similar? As they are fetched using the magic methods anyway, perhaps I can add the relationship dynamically?

                  谢谢!

                  推荐答案

                  我为此添加了一个包i-rocky/eloquent-dynamic-relation

                  如果有人仍在寻找解决方案,这里有一个.如果您认为这是个坏主意,请告诉我.

                  In case anyone still looking for a solution , here is one. If you think it's a bad idea, let me know.

                  trait HasDynamicRelation
                  {
                      /**
                       * Store the relations
                       *
                       * @var array
                       */
                      private static $dynamic_relations = [];
                  
                      /**
                       * Add a new relation
                       *
                       * @param $name
                       * @param $closure
                       */
                      public static function addDynamicRelation($name, $closure)
                      {
                          static::$dynamic_relations[$name] = $closure;
                      }
                  
                      /**
                       * Determine if a relation exists in dynamic relationships list
                       *
                       * @param $name
                       *
                       * @return bool
                       */
                      public static function hasDynamicRelation($name)
                      {
                          return array_key_exists($name, static::$dynamic_relations);
                      }
                  
                      /**
                       * If the key exists in relations then
                       * return call to relation or else
                       * return the call to the parent
                       *
                       * @param $name
                       *
                       * @return mixed
                       */
                      public function __get($name)
                      {
                          if (static::hasDynamicRelation($name)) {
                              // check the cache first
                              if ($this->relationLoaded($name)) {
                                  return $this->relations[$name];
                              }
                  
                              // load the relationship
                              return $this->getRelationshipFromMethod($name);
                          }
                  
                          return parent::__get($name);
                      }
                  
                      /**
                       * If the method exists in relations then
                       * return the relation or else
                       * return the call to the parent
                       *
                       * @param $name
                       * @param $arguments
                       *
                       * @return mixed
                       */
                      public function __call($name, $arguments)
                      {
                          if (static::hasDynamicRelation($name)) {
                              return call_user_func(static::$dynamic_relations[$name], $this);
                          }
                  
                          return parent::__call($name, $arguments);
                      }
                  }
                  

                  在您的模型中添加此特征,如下所示

                  Add this trait in your model as following

                  class MyModel extends Model {
                      use HasDynamicRelation;
                  }
                  

                  现在您可以使用以下方法添加新关系

                  Now you can use the following method to add new relationships

                  MyModel::addDynamicRelation('some_relation', function(MyModel $model) {
                      return $model->hasMany(SomeRelatedModel::class);
                  });
                  

                  这篇关于Laravel - Eloquent - 动态定义的关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Laravel 5.5 对具有不同列名的单独表的唯一验证规 下一篇:Laravel Eloquent 关于关系的分页

                  相关文章

                  最新文章

                  1. <legend id='7o8aS'><style id='7o8aS'><dir id='7o8aS'><q id='7o8aS'></q></dir></style></legend>

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

                      <small id='7o8aS'></small><noframes id='7o8aS'>

                        <bdo id='7o8aS'></bdo><ul id='7o8aS'></ul>