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

    <tfoot id='Unixb'></tfoot>

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

        <legend id='Unixb'><style id='Unixb'><dir id='Unixb'><q id='Unixb'></q></dir></style></legend>

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

      1. 如何为 Laravel/Eloquent 模型设置默认属性值?

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

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

            <tbody id='hQV5r'></tbody>
              <bdo id='hQV5r'></bdo><ul id='hQV5r'></ul>

                  <tfoot id='hQV5r'></tfoot>

                1. 本文介绍了如何为 Laravel/Eloquent 模型设置默认属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  如果我尝试声明一个属性,如下所示:

                  public $quantity = 9;

                  ...它不起作用,因为它不被视为属性",而只是模型类的属性.不仅如此,我还阻止了对实际真实存在的数量"属性的访问.

                  那我该怎么办?

                  解决方案

                  这就是我现在正在做的:

                  protected $defaults = array('数量' =>9、);公共函数 __construct(array $attributes = array()){$this->setRawAttributes($this->defaults, true);parent::__construct($attributes);}

                  我建议将此作为 PR,因此我们不需要在每个模型中都声明此构造函数,并且只需在我们的模型中声明 $defaults 数组即可轻松应用...<小时>

                  更新:

                  正如 cmfolio 所指出的,实际的答案很简单:

                  只需覆盖 $attributes 属性即可!像这样:

                  protected $attributes = array('数量' =>9、);

                  在此处讨论了该问题.

                  If I try declaring a property, like this:

                  public $quantity = 9;
                  

                  ...it doesn't work, because it is not considered an "attribute", but merely a property of the model class. Not only this, but also I am blocking access to the actually real and existent "quantity" attribute.

                  What should I do, then?

                  解决方案

                  This is what I'm doing now:

                  protected $defaults = array(
                     'quantity' => 9,
                  );
                  
                  public function __construct(array $attributes = array())
                  {
                      $this->setRawAttributes($this->defaults, true);
                      parent::__construct($attributes);
                  }
                  

                  I will suggest this as a PR so we don't need to declare this constructor at every Model, and can easily apply by simply declaring the $defaults array in our models...


                  UPDATE:

                  As pointed by cmfolio, the actual ANSWER is quite simple:

                  Just override the $attributes property! Like this:

                  protected $attributes = array(
                     'quantity' => 9,
                  );
                  

                  The issue was discussed here.

                  这篇关于如何为 Laravel/Eloquent 模型设置默认属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:按多列对 Eloquent 集合进行排序的语法是什么? 下一篇:Laravel Eloquent - 附加与同步

                  相关文章

                  最新文章

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

                  <legend id='qFaD2'><style id='qFaD2'><dir id='qFaD2'><q id='qFaD2'></q></dir></style></legend>

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

                    • <bdo id='qFaD2'></bdo><ul id='qFaD2'></ul>