{"id":9434,"date":"2021-01-12T10:00:59","date_gmt":"2021-01-12T01:00:59","guid":{"rendered":"https:\/\/www.gigas-jp.com\/appnews\/?p=9434"},"modified":"2021-01-08T20:56:40","modified_gmt":"2021-01-08T11:56:40","slug":"using-controller-function-directly-in-laravel-blade","status":"publish","type":"post","link":"https:\/\/www.gigas-jp.com\/appnews\/archives\/9434","title":{"rendered":"Using controller function directly in Laravel blade"},"content":{"rendered":"\n<p>Today I would like to talk about using the function from controller directly on your blade view files.<\/p>\n\n\n\n<p>Normally we wrote codes relating with application logic and database and then pass the data to specific view files. But what if we like to use the function from controller directly ?<\/p>\n\n\n\n<p>We can create a static function in controller and then can directly be called from blade file without instantiating the class. Let me write a sample example.<\/p>\n\n\n\n<p>Firstly, we will create a static function in controller. Why static function ? <\/p>\n\n\n\n<p>If we create a normal function, we will need to instantiate the class in view file like this `new Controller()` which will be a mess in view files. Static function can be directly called using scope resolution (::) operator.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\n\nnamespace App\\Http\\Controllers;\n\nclass HomeController extends Controller\n{\n    public static function greet() \n    {\n        return \"Hello World\";\n    }\n}\n<\/code><\/pre>\n\n\n\n<p>After that we can call the above controller function directly by providing the namespace of the controller and function using scope resolution operator like this.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;p>                                    \n{{ App\\Http\\Controllers\\HomeController::greet() }}\n&lt;!-- output - Hello World -->\n&lt;\/p><\/code><\/pre>\n\n\n\n<p>By Yuuma<\/p>\n<div class='wp_social_bookmarking_light'>\n            <div class=\"wsbl_google_plus_one\"><g:plusone size=\"medium\" annotation=\"none\" href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9434\" ><\/g:plusone><\/div>\n            <div class=\"wsbl_hatena_button\"><a href=\"\/\/b.hatena.ne.jp\/entry\/https:\/\/www.gigas-jp.com\/appnews\/archives\/9434\" class=\"hatena-bookmark-button\" data-hatena-bookmark-title=\"Using controller function directly in Laravel blade\" data-hatena-bookmark-layout=\"standard\" title=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\"> <img src=\"\/\/b.hatena.ne.jp\/images\/entry-button\/button-only@2x.png\" alt=\"\u3053\u306e\u30a8\u30f3\u30c8\u30ea\u30fc\u3092\u306f\u3066\u306a\u30d6\u30c3\u30af\u30de\u30fc\u30af\u306b\u8ffd\u52a0\" width=\"20\" height=\"20\" style=\"border: none;\" \/><\/a><script type=\"text\/javascript\" src=\"\/\/b.hatena.ne.jp\/js\/bookmark_button.js\" charset=\"utf-8\" async=\"async\"><\/script><\/div>\n            <div class=\"wsbl_twitter\"><a href=\"https:\/\/twitter.com\/share\" class=\"twitter-share-button\" data-url=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9434\" data-text=\"Using controller function directly in Laravel blade\" data-via=\"GIGASJAPAN_APPS\" data-lang=\"ja\">Tweet<\/a><\/div>\n            <div class=\"wsbl_facebook_like\"><div id=\"fb-root\"><\/div><fb:like href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9434\" layout=\"button_count\" action=\"like\" width=\"100\" share=\"false\" show_faces=\"false\" ><\/fb:like><\/div>\n            <div class=\"wsbl_facebook_send\"><div id=\"fb-root\"><\/div><fb:send href=\"https:\/\/www.gigas-jp.com\/appnews\/archives\/9434\" colorscheme=\"light\" ><\/fb:send><\/div>\n    <\/div>\n<br class='wp_social_bookmarking_light_clear' \/>\n","protected":false},"excerpt":{"rendered":"<p>Today I would like to talk about using the function from controller directly on your blade view files. Normall [&hellip;]<\/p>\n","protected":false},"author":18,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[100],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9434"}],"collection":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/users\/18"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/comments?post=9434"}],"version-history":[{"count":2,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9434\/revisions"}],"predecessor-version":[{"id":9438,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/posts\/9434\/revisions\/9438"}],"wp:attachment":[{"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/media?parent=9434"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/categories?post=9434"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gigas-jp.com\/appnews\/wp-json\/wp\/v2\/tags?post=9434"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}