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

    1. <tfoot id='rur6u'></tfoot>

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

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

      2. Laravel 如何在 Eloquent 模型中添加自定义函数?

        时间:2023-09-23
        1. <i id='MxI3D'><tr id='MxI3D'><dt id='MxI3D'><q id='MxI3D'><span id='MxI3D'><b id='MxI3D'><form id='MxI3D'><ins id='MxI3D'></ins><ul id='MxI3D'></ul><sub id='MxI3D'></sub></form><legend id='MxI3D'></legend><bdo id='MxI3D'><pre id='MxI3D'><center id='MxI3D'></center></pre></bdo></b><th id='MxI3D'></th></span></q></dt></tr></i><div id='MxI3D'><tfoot id='MxI3D'></tfoot><dl id='MxI3D'><fieldset id='MxI3D'></fieldset></dl></div>
            <bdo id='MxI3D'></bdo><ul id='MxI3D'></ul>

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

          • <legend id='MxI3D'><style id='MxI3D'><dir id='MxI3D'><q id='MxI3D'></q></dir></style></legend>

                <tbody id='MxI3D'></tbody>
                • <tfoot id='MxI3D'></tfoot>
                  本文介绍了Laravel 如何在 Eloquent 模型中添加自定义函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个产品模型

                  class Product extends Model
                  {
                      ...
                  
                      public function prices()
                      {
                          return $this->hasMany('AppPrice');
                      }
                  
                      ...
                  }
                  

                  我想添加一个返回最低价格的函数,在控制器中我可以使用以下方法获取值:

                  I want to add a function which will return the lowest price, and in controller I can get the value using:

                  Product::find(1)->lowest;
                  

                  我在产品模型中添加了这个:

                  I added this in Product model:

                  public function lowest()
                  {
                      return $this->prices->min('price');
                  }
                  

                  但我收到一条错误消息:

                  but I got an error saying:

                  Relationship method must return an object of type IlluminateDatabaseEloquentRelationsRelation
                  

                  如果我使用 Product::find(1)->lowest();,它会起作用.是否可以让 Product::find(1)->lowest; 工作?

                  And if I use Product::find(1)->lowest();, it will work. Is it possible to get Product::find(1)->lowest; to work?

                  任何帮助将不胜感激.

                  推荐答案

                  当您尝试将模型中的函数作为变量访问时,laravel 假定您正在尝试检索相关模型.他们称它们为动态属性.您需要的是自定义属性.

                  When you try to access a function in the model as a variable, laravel assumes you're trying to retrieve a related model. They call them dynamic properties. What you need instead is a custom attribute.

                  Laravel 6 文档:https://laravel.com/docs/6.x/雄辩的变异者

                  Laravel 6 docs: https://laravel.com/docs/6.x/eloquent-mutators

                  将以下方法添加到您的模型中:

                  add following method to your model:

                  public function getLowestAttribute()
                  {
                      //do whatever you want to do
                      return 'lowest price';
                  }
                  

                  现在您应该可以像这样访问它:

                  Now you should be able to access it like this:

                  Product::find(1)->lowest;
                  

                  这篇关于Laravel 如何在 Eloquent 模型中添加自定义函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:具有多个条件的 Laravel Eloquent 内连接 下一篇:如何使用 Laravel 4.1 基于 id 或对象数组执行批量删

                  相关文章

                  最新文章

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

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

                    1. <tfoot id='JcEZZ'></tfoot>