How to write a custom function in a model?










0















There is a model data:



class Order extends Model




How to write a custom method inside the Order class so that it can be called in constructor like this:



Order::myMethod()
Order->myMethod()


Where myMethod is:



public function myMethod() 
return DB::query(<SQL QUERY>);



Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.










share|improve this question

















  • 2





    Read the docs on query scopes.

    – Devon
    Nov 15 '18 at 1:21











  • This looks like an XY problem . Can you provide more infomation on exactly what you need myMethod to do?

    – apokryfos
    Nov 15 '18 at 1:26











  • What you need is Repository Design Pattern.

    – Kenny
    Nov 15 '18 at 2:09











  • Curious, why not use Eloquent?

    – wheelmaker
    Nov 15 '18 at 2:47















0















There is a model data:



class Order extends Model




How to write a custom method inside the Order class so that it can be called in constructor like this:



Order::myMethod()
Order->myMethod()


Where myMethod is:



public function myMethod() 
return DB::query(<SQL QUERY>);



Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.










share|improve this question

















  • 2





    Read the docs on query scopes.

    – Devon
    Nov 15 '18 at 1:21











  • This looks like an XY problem . Can you provide more infomation on exactly what you need myMethod to do?

    – apokryfos
    Nov 15 '18 at 1:26











  • What you need is Repository Design Pattern.

    – Kenny
    Nov 15 '18 at 2:09











  • Curious, why not use Eloquent?

    – wheelmaker
    Nov 15 '18 at 2:47













0












0








0


1






There is a model data:



class Order extends Model




How to write a custom method inside the Order class so that it can be called in constructor like this:



Order::myMethod()
Order->myMethod()


Where myMethod is:



public function myMethod() 
return DB::query(<SQL QUERY>);



Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.










share|improve this question














There is a model data:



class Order extends Model




How to write a custom method inside the Order class so that it can be called in constructor like this:



Order::myMethod()
Order->myMethod()


Where myMethod is:



public function myMethod() 
return DB::query(<SQL QUERY>);



Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.







laravel laravel-5






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 0:19









OPVOPV

1,68221338




1,68221338







  • 2





    Read the docs on query scopes.

    – Devon
    Nov 15 '18 at 1:21











  • This looks like an XY problem . Can you provide more infomation on exactly what you need myMethod to do?

    – apokryfos
    Nov 15 '18 at 1:26











  • What you need is Repository Design Pattern.

    – Kenny
    Nov 15 '18 at 2:09











  • Curious, why not use Eloquent?

    – wheelmaker
    Nov 15 '18 at 2:47












  • 2





    Read the docs on query scopes.

    – Devon
    Nov 15 '18 at 1:21











  • This looks like an XY problem . Can you provide more infomation on exactly what you need myMethod to do?

    – apokryfos
    Nov 15 '18 at 1:26











  • What you need is Repository Design Pattern.

    – Kenny
    Nov 15 '18 at 2:09











  • Curious, why not use Eloquent?

    – wheelmaker
    Nov 15 '18 at 2:47







2




2





Read the docs on query scopes.

– Devon
Nov 15 '18 at 1:21





Read the docs on query scopes.

– Devon
Nov 15 '18 at 1:21













This looks like an XY problem . Can you provide more infomation on exactly what you need myMethod to do?

– apokryfos
Nov 15 '18 at 1:26





This looks like an XY problem . Can you provide more infomation on exactly what you need myMethod to do?

– apokryfos
Nov 15 '18 at 1:26













What you need is Repository Design Pattern.

– Kenny
Nov 15 '18 at 2:09





What you need is Repository Design Pattern.

– Kenny
Nov 15 '18 at 2:09













Curious, why not use Eloquent?

– wheelmaker
Nov 15 '18 at 2:47





Curious, why not use Eloquent?

– wheelmaker
Nov 15 '18 at 2:47












5 Answers
5






active

oldest

votes


















2














Guess you are asking about the static functions:



class Order extends Model 
public static function myMethod()




and you can call it anywhere like



Order::myMethod();





share|improve this answer






























    3














    Rather create a custom function in Model, You can use traits to achieve the desired output.



    Please follow either steps:-



    • https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3

    • https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5





    share|improve this answer























    • Welcome ........

      – Mayank Majithya
      Nov 16 '18 at 5:39


















    1














    I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this



    class ABC extends Model

    //here is your fillable array;
    public function abc()

    //Here is your Eloquent statement or SQL query;




    just call this abc() method inside the controller like this



     use ABC;
    class AbcController extends Controller

    private $_abc; // it is private variable
    // this is constructor
    public function __construct(ABC $abc)

    $this->_abc= $abc;

    public function abcMethod()

    $this->_abc->abc();




    Thanks






    share|improve this answer






























      1














      You can achieve the desired behavior using magic methods __call and __callStatic
      if your real method is static you can use __call() to intercept all "non static" calls and use it to call the static and use __callStatic to forward the calls to a new instance to that class .



      Your methods should be always static because if a non static method exists and you are calling it statically php raises an error



      Non-static method Foo::myMethod() should not be called statically


      No problem if your method is static



      class Order extends Model 
      public static function myMethod()
      return static::query()->where(...)->get(); // example

      public function __call($name, $arguments)
      return forward_static_call_array([__CLASS__, $name], $arguments);

      public static function __callStatic($name, $arguments)
      return call_user_func_array([app(__CLASS__), $name], $arguments);



      (new Order())->myMethod();
      Order::myMethod();





      share|improve this answer






























        0














        I don't believe I'm understanding your intention. You've stated:




        Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.




        Why does the Order->myMethod() need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.






        share|improve this answer






















          Your Answer






          StackExchange.ifUsing("editor", function ()
          StackExchange.using("externalEditor", function ()
          StackExchange.using("snippets", function ()
          StackExchange.snippets.init();
          );
          );
          , "code-snippets");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "1"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader:
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          ,
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













          draft saved

          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310720%2fhow-to-write-a-custom-function-in-a-model%23new-answer', 'question_page');

          );

          Post as a guest















          Required, but never shown

























          5 Answers
          5






          active

          oldest

          votes








          5 Answers
          5






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          2














          Guess you are asking about the static functions:



          class Order extends Model 
          public static function myMethod()




          and you can call it anywhere like



          Order::myMethod();





          share|improve this answer



























            2














            Guess you are asking about the static functions:



            class Order extends Model 
            public static function myMethod()




            and you can call it anywhere like



            Order::myMethod();





            share|improve this answer

























              2












              2








              2







              Guess you are asking about the static functions:



              class Order extends Model 
              public static function myMethod()




              and you can call it anywhere like



              Order::myMethod();





              share|improve this answer













              Guess you are asking about the static functions:



              class Order extends Model 
              public static function myMethod()




              and you can call it anywhere like



              Order::myMethod();






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 15 '18 at 3:54









              Baraa Al-TabbaaBaraa Al-Tabbaa

              66558




              66558























                  3














                  Rather create a custom function in Model, You can use traits to achieve the desired output.



                  Please follow either steps:-



                  • https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3

                  • https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5





                  share|improve this answer























                  • Welcome ........

                    – Mayank Majithya
                    Nov 16 '18 at 5:39















                  3














                  Rather create a custom function in Model, You can use traits to achieve the desired output.



                  Please follow either steps:-



                  • https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3

                  • https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5





                  share|improve this answer























                  • Welcome ........

                    – Mayank Majithya
                    Nov 16 '18 at 5:39













                  3












                  3








                  3







                  Rather create a custom function in Model, You can use traits to achieve the desired output.



                  Please follow either steps:-



                  • https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3

                  • https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5





                  share|improve this answer













                  Rather create a custom function in Model, You can use traits to achieve the desired output.



                  Please follow either steps:-



                  • https://medium.com/@kshitij206/traits-in-laravel-5db8beffbcc3

                  • https://www.conetix.com.au/blog/simple-guide-using-traits-laravel-5






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Nov 15 '18 at 5:42









                  Mayank MajithyaMayank Majithya

                  1,064515




                  1,064515












                  • Welcome ........

                    – Mayank Majithya
                    Nov 16 '18 at 5:39

















                  • Welcome ........

                    – Mayank Majithya
                    Nov 16 '18 at 5:39
















                  Welcome ........

                  – Mayank Majithya
                  Nov 16 '18 at 5:39





                  Welcome ........

                  – Mayank Majithya
                  Nov 16 '18 at 5:39











                  1














                  I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this



                  class ABC extends Model

                  //here is your fillable array;
                  public function abc()

                  //Here is your Eloquent statement or SQL query;




                  just call this abc() method inside the controller like this



                   use ABC;
                  class AbcController extends Controller

                  private $_abc; // it is private variable
                  // this is constructor
                  public function __construct(ABC $abc)

                  $this->_abc= $abc;

                  public function abcMethod()

                  $this->_abc->abc();




                  Thanks






                  share|improve this answer



























                    1














                    I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this



                    class ABC extends Model

                    //here is your fillable array;
                    public function abc()

                    //Here is your Eloquent statement or SQL query;




                    just call this abc() method inside the controller like this



                     use ABC;
                    class AbcController extends Controller

                    private $_abc; // it is private variable
                    // this is constructor
                    public function __construct(ABC $abc)

                    $this->_abc= $abc;

                    public function abcMethod()

                    $this->_abc->abc();




                    Thanks






                    share|improve this answer

























                      1












                      1








                      1







                      I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this



                      class ABC extends Model

                      //here is your fillable array;
                      public function abc()

                      //Here is your Eloquent statement or SQL query;




                      just call this abc() method inside the controller like this



                       use ABC;
                      class AbcController extends Controller

                      private $_abc; // it is private variable
                      // this is constructor
                      public function __construct(ABC $abc)

                      $this->_abc= $abc;

                      public function abcMethod()

                      $this->_abc->abc();




                      Thanks






                      share|improve this answer













                      I can't understand your exact problem is. but if you are using laravel, then you can write custom method inside the ABC model like this



                      class ABC extends Model

                      //here is your fillable array;
                      public function abc()

                      //Here is your Eloquent statement or SQL query;




                      just call this abc() method inside the controller like this



                       use ABC;
                      class AbcController extends Controller

                      private $_abc; // it is private variable
                      // this is constructor
                      public function __construct(ABC $abc)

                      $this->_abc= $abc;

                      public function abcMethod()

                      $this->_abc->abc();




                      Thanks







                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 15 '18 at 8:28









                      Zeeshan TanveerZeeshan Tanveer

                      313




                      313





















                          1














                          You can achieve the desired behavior using magic methods __call and __callStatic
                          if your real method is static you can use __call() to intercept all "non static" calls and use it to call the static and use __callStatic to forward the calls to a new instance to that class .



                          Your methods should be always static because if a non static method exists and you are calling it statically php raises an error



                          Non-static method Foo::myMethod() should not be called statically


                          No problem if your method is static



                          class Order extends Model 
                          public static function myMethod()
                          return static::query()->where(...)->get(); // example

                          public function __call($name, $arguments)
                          return forward_static_call_array([__CLASS__, $name], $arguments);

                          public static function __callStatic($name, $arguments)
                          return call_user_func_array([app(__CLASS__), $name], $arguments);



                          (new Order())->myMethod();
                          Order::myMethod();





                          share|improve this answer



























                            1














                            You can achieve the desired behavior using magic methods __call and __callStatic
                            if your real method is static you can use __call() to intercept all "non static" calls and use it to call the static and use __callStatic to forward the calls to a new instance to that class .



                            Your methods should be always static because if a non static method exists and you are calling it statically php raises an error



                            Non-static method Foo::myMethod() should not be called statically


                            No problem if your method is static



                            class Order extends Model 
                            public static function myMethod()
                            return static::query()->where(...)->get(); // example

                            public function __call($name, $arguments)
                            return forward_static_call_array([__CLASS__, $name], $arguments);

                            public static function __callStatic($name, $arguments)
                            return call_user_func_array([app(__CLASS__), $name], $arguments);



                            (new Order())->myMethod();
                            Order::myMethod();





                            share|improve this answer

























                              1












                              1








                              1







                              You can achieve the desired behavior using magic methods __call and __callStatic
                              if your real method is static you can use __call() to intercept all "non static" calls and use it to call the static and use __callStatic to forward the calls to a new instance to that class .



                              Your methods should be always static because if a non static method exists and you are calling it statically php raises an error



                              Non-static method Foo::myMethod() should not be called statically


                              No problem if your method is static



                              class Order extends Model 
                              public static function myMethod()
                              return static::query()->where(...)->get(); // example

                              public function __call($name, $arguments)
                              return forward_static_call_array([__CLASS__, $name], $arguments);

                              public static function __callStatic($name, $arguments)
                              return call_user_func_array([app(__CLASS__), $name], $arguments);



                              (new Order())->myMethod();
                              Order::myMethod();





                              share|improve this answer













                              You can achieve the desired behavior using magic methods __call and __callStatic
                              if your real method is static you can use __call() to intercept all "non static" calls and use it to call the static and use __callStatic to forward the calls to a new instance to that class .



                              Your methods should be always static because if a non static method exists and you are calling it statically php raises an error



                              Non-static method Foo::myMethod() should not be called statically


                              No problem if your method is static



                              class Order extends Model 
                              public static function myMethod()
                              return static::query()->where(...)->get(); // example

                              public function __call($name, $arguments)
                              return forward_static_call_array([__CLASS__, $name], $arguments);

                              public static function __callStatic($name, $arguments)
                              return call_user_func_array([app(__CLASS__), $name], $arguments);



                              (new Order())->myMethod();
                              Order::myMethod();






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 15 '18 at 9:25









                              simonecoscisimonecosci

                              82658




                              82658





















                                  0














                                  I don't believe I'm understanding your intention. You've stated:




                                  Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.




                                  Why does the Order->myMethod() need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.






                                  share|improve this answer



























                                    0














                                    I don't believe I'm understanding your intention. You've stated:




                                    Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.




                                    Why does the Order->myMethod() need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.






                                    share|improve this answer

























                                      0












                                      0








                                      0







                                      I don't believe I'm understanding your intention. You've stated:




                                      Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.




                                      Why does the Order->myMethod() need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.






                                      share|improve this answer













                                      I don't believe I'm understanding your intention. You've stated:




                                      Purpose is to move SQL queries inside model's class, that don't mess this code in controllers.




                                      Why does the Order->myMethod() need calling inside the constructor? If you're trying to design your data access layer to work efficiently, you can use data repositories.







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 15 '18 at 1:13









                                      parseMaestro63parseMaestro63

                                      816




                                      816



























                                          draft saved

                                          draft discarded
















































                                          Thanks for contributing an answer to Stack Overflow!


                                          • Please be sure to answer the question. Provide details and share your research!

                                          But avoid


                                          • Asking for help, clarification, or responding to other answers.

                                          • Making statements based on opinion; back them up with references or personal experience.

                                          To learn more, see our tips on writing great answers.




                                          draft saved


                                          draft discarded














                                          StackExchange.ready(
                                          function ()
                                          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53310720%2fhow-to-write-a-custom-function-in-a-model%23new-answer', 'question_page');

                                          );

                                          Post as a guest















                                          Required, but never shown





















































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown

































                                          Required, but never shown














                                          Required, but never shown












                                          Required, but never shown







                                          Required, but never shown







                                          這個網誌中的熱門文章

                                          Barbados

                                          How to read a connectionString WITH PROVIDER in .NET Core?

                                          Node.js Script on GitHub Pages or Amazon S3