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

    2. <legend id='9oaDK'><style id='9oaDK'><dir id='9oaDK'><q id='9oaDK'></q></dir></style></legend>

      <small id='9oaDK'></small><noframes id='9oaDK'>

        <tfoot id='9oaDK'></tfoot>

          <bdo id='9oaDK'></bdo><ul id='9oaDK'></ul>

        在 Laravel 中创建一个可链接的方法

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

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

                  <tbody id='I5qDA'></tbody>

                  本文介绍了在 Laravel 中创建一个可链接的方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我一直在尝试在 laravel' eloquent 中创建我自己的可链接方法,但我遗漏了一些东西并且不确定是什么.这听起来可能有点疯狂,但请查看下面我的函数,以更好地了解我想说的内容.

                  I have been trying to create my own chainable method in laravel' eloquent but I'm missing something and not sure what. This may sound a little bit nuts but have a look at my function below to get a better idea of what I'm trying to say.

                  class Post extends Eloquent{
                      public static function custom_wh($data){
                          return static::where_in('categories_id', $data, 'AND');
                      }
                  }
                  
                  //this works fine
                  $posts = Post::custom_wh(array(1, 2, 3))->get();
                  
                  //but this says custom_wh is not defined in the query class
                  $posts = Post::where_in('tags', array(2, 3, 4), 'AND')->custom_wh(array(1, 2, 3))->get();
                  

                  如果我理解正确,那么我的方法没有资格在另一个方法之后链接?所以我想我的问题是如何在我的模型中创建一个可链接的方法?

                  if I understand correctly then my method is not eligible to chain after another method? So I guess my question is how can I create a chainable method in my model?

                  PS 我已经查看了 laravel 的查询构建器类,在那里我看到可链接的方法返回该对象的实例,但除了我在代码中完成的方式之外,我找不到返回该对象的方法多于.任何形式的建议或建议都非常感谢.提前致谢.

                  P.S I have looked into the laravel's query builder class where I have seen that the chainable methods returns the instance of that object but I couldn't find a way to return the object other than the way I've done in the code above. Any kind of suggestion or advice is highly appreciated. Thanks in advance.

                  推荐答案

                  您可以在 Laravel 中使用查询范围"来做到这一点.您可以在此处找到文档.

                  You can do that in Laravel with the "query scopes". You can find the doc here.

                  你只需要写一个带有前缀scope的函数,你就可以像其他查询构建器一样链接这个方法:

                  You just have to write a function with the prefix scope and you will be able to chain this method like the other query builder ones :

                  class Post extends Eloquent {
                  
                      public function scopeWhereCategories($query, $categories)
                      {
                          return $query->whereIn('categories_id', $categories, 'AND');
                      }
                  
                  }
                  
                  $posts = Post::whereCategories([1, 2, 3])->get();
                  $posts = Post::orderBy('date')->whereCategories([1, 2, 3])->take(5)->get();
                  

                  这篇关于在 Laravel 中创建一个可链接的方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:laravel 4:验证唯一(数据库)多个 where 子句 下一篇:Laravel Carbon Group 按月

                  相关文章

                  最新文章

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

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

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

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