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

      <legend id='yYHLr'><style id='yYHLr'><dir id='yYHLr'><q id='yYHLr'></q></dir></style></legend>
    1. <small id='yYHLr'></small><noframes id='yYHLr'>

      Laravel 获取相关模型的类名

      时间:2023-09-23
      • <legend id='lcYyS'><style id='lcYyS'><dir id='lcYyS'><q id='lcYyS'></q></dir></style></legend>

        <tfoot id='lcYyS'></tfoot>
          <tbody id='lcYyS'></tbody>

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

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

                <bdo id='lcYyS'></bdo><ul id='lcYyS'></ul>
              • 本文介绍了Laravel 获取相关模型的类名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                在我的 Laravel 应用程序中,我有一个 Faq 模型.一个 Faq 模型可以包含多个 Product 模型,所以 Faq 类包含以下函数:

                In my Laravel application I have an Faq model. An Faq model can contain many Product models, so the Faq class contains the following function:

                class Faq extends Eloquent{ 
                    public function products(){
                        return $this->belongsToMany('Product');
                    }
                }
                

                在控制器中,我希望能够检索定义关系的类名.例如,如果我有一个 Faq 对象,如下所示:

                In a controller, I would like to be able to retrieve the class name that defines the relationship. For example, if I have an Faq object, like this:

                $faq = new Faq();
                

                如何确定关系的类名,在本例中为 Product.目前我可以这样做:

                How can I determine the class name of the relationship, which in this case would be Product. Currently I am able to do it like this:

                $className = get_class($faq->products()->get()->first());
                

                但是,我想知道是否有一种方法可以在无需实际运行查询的情况下完成同样的事情.

                However, I'm wondering if there is a way to accomplish this same thing without having to actually run a query.

                推荐答案

                是的,有一种不用查询就可以得到相关模型的方法:

                Yes, there is a way to get related model without query:

                $className = get_class($faq->products()->getRelated());
                

                它适用于所有关系.

                这将返回带有命名空间的全名.如果您只想使用基本名称:

                This will return full name with namespace. In case you want just base name use:

                // laravel helper:
                $baseClass = class_basename($className);
                
                // generic solution
                $reflection = new ReflectionClass($className);
                $reflection->getShortName();
                

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

                上一篇:选择前 10 行 - Laravel Eloquent 下一篇:laravel Eloquent ORM delete() 方法

                相关文章

                最新文章

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

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

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