{"id":23,"date":"2021-01-14T07:38:42","date_gmt":"2021-01-14T07:38:42","guid":{"rendered":"https:\/\/mouryasolutions.in\/blog\/?p=23"},"modified":"2023-01-29T18:41:57","modified_gmt":"2023-01-29T18:41:57","slug":"call-static-method-directly-from-route","status":"publish","type":"post","link":"https:\/\/mouryasolutions.in\/blog\/call-static-method-directly-from-route\/","title":{"rendered":"How to call a static method of a class directly from a route in Laravel"},"content":{"rendered":"<div class=\"post-content\">\n<blockquote class=\"wp-block-quote\">\n<p>In Laravel, you can call a static method of a class directly from a route. Here is an example of how to do this:<\/p>\n<\/blockquote>\n\n\n\n<h4>Create a static method in a class. For example, create a class <code>Example<\/code> with a static method <code>hello()<\/code>:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>class Example {\r\n    public static function hello() {\r\n        return 'Hello from Example class!';\r\n    }\r\n}\r<\/code><\/pre>\n\n\n\n<h4>In your <code>routes\/web.php<\/code> file, define a route that calls the static method:<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>Route::get('example', function () {\r\n    return Example::hello();\r\n});\r<\/code><\/pre>\n\n\n\n<p>Now, when you visit <code>http:\/\/your-app.test\/example<\/code> in your browser, you will see the message &#8220;Hello from Example class!&#8221; displayed.<\/p>\n\n\n\n<p>Note: Replace <code>your-app.test<\/code> with the actual URL of your Laravel application.<\/p>\n<\/div><!-- AddThis Advanced Settings generic via filter on the_content --><!-- AddThis Share Buttons generic via filter on the_content -->","protected":false},"excerpt":{"rendered":"<p>In Laravel, you can call a static method of a<a href=\"https:\/\/mouryasolutions.in\/blog\/call-static-method-directly-from-route\/\">Read More<i class=\"fa fa-long-arrow-right\" aria-hidden=\"true\"><\/i><\/a><!-- AddThis Advanced Settings generic via filter on get_the_excerpt --><!-- AddThis Share Buttons generic via filter on get_the_excerpt --><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[2],"tags":[],"_links":{"self":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/23"}],"collection":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/comments?post=23"}],"version-history":[{"count":3,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions"}],"predecessor-version":[{"id":494,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/posts\/23\/revisions\/494"}],"wp:attachment":[{"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/media?parent=23"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/categories?post=23"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mouryasolutions.in\/blog\/wp-json\/wp\/v2\/tags?post=23"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}