{"id":1064,"date":"2023-09-28T05:33:00","date_gmt":"2023-09-28T05:33:00","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1064"},"modified":"2023-09-28T05:33:02","modified_gmt":"2023-09-28T05:33:02","slug":"what-is-constructor-use-of-constructor-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/","title":{"rendered":"What is constructor ? Use of Constructor in laravel ?"},"content":{"rendered":"\n<p>A constructor is a special method that is called automatically when an object of a class is created. It is used to initialize the object&#8217;s properties and perform any other necessary setup tasks.<\/p>\n\n\n\n<p>In Laravel, constructors are used for a variety of purposes, including:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Dependency injection:<\/strong>&nbsp;Laravel uses constructors to inject dependencies into your classes. This means that you can specify the dependencies that your class needs in its constructor, and Laravel will automatically resolve them and inject them into the object.<\/li>\n\n\n\n<li><strong>Initialization:<\/strong>&nbsp;Constructors can also be used to initialize the object&#8217;s properties. For example, you could use the constructor to set the object&#8217;s name, email address, or other default values.<\/li>\n\n\n\n<li><strong>Setup:<\/strong>&nbsp;Constructors can also be used to perform any other necessary setup tasks, such as connecting to a database or initializing a logger.<\/li>\n<\/ul>\n\n\n\n<p>To use a constructor in Laravel, simply create a method called <code>__construct()<\/code> in your class. You can then type-hint any dependencies that your class needs in the constructor. For example, the following constructor injects a <code>UserRepository<\/code> class into the <code>UserController<\/code> class:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse Illuminate\\Http\\Request;\r\n\r\nclass UserController extends Controller\r\n{\r\n    public function __construct()\r\n    {\r\n        $this->middleware('auth'); \/\/ This middleware will apply to all methods in the controller\r\n    }\r\n\r\n    public function index()\r\n    {\r\n        \/\/ Your code for handling the index route\r\n    }\r\n\r\n    \/\/ Other methods...\r\n}\r<\/code><\/pre>\n\n\n\n<p>In this example, the <code>UserController<\/code> class has a constructor defined with <code>public function __construct()<\/code>. Inside the constructor, we&#8217;re using <code>$this->middleware('auth')<\/code> to apply the &#8216;auth&#8217; middleware to all methods in the controller. This means that before any action in this controller is executed, Laravel will first check if the user is authenticated.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\nuse App\\Services\\UserService;\r\n\r\nclass UserController extends Controller\r\n{\r\n    protected $userService;\r\n\r\n    public function __construct(UserService $userService)\r\n    {\r\n        $this->userService = $userService;\r\n    }\r\n\r\n    public function index()\r\n    {\r\n        $users = $this->userService->getAllUsers();\r\n        return view('users.index', compact('users'));\r\n    }\r\n\r\n    \/\/ Other methods...\r\n}\r<\/code><\/pre>\n\n\n\n<p>Thanks for learning \ud83d\udc4d\ud83d\udc4d<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A constructor is a special method that is called automatically when an object of a class is created. It is used to initialize the object&#8217;s properties and&#8230; <\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[25],"tags":[377,3,432,416],"class_list":["post-1064","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-constructor-function","tag-laravel","tag-php-function","tag-protected-class"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.4 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>What is constructor ? Use of Constructor in laravel ? - DevOps Freelancer<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What is constructor ? Use of Constructor in laravel ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"A constructor is a special method that is called automatically when an object of a class is created. It is used to initialize the object&#8217;s properties and...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"DevOps Freelancer\" \/>\n<meta property=\"article:author\" content=\"https:\/\/www.facebook.com\/amitsthakurs\/\" \/>\n<meta property=\"article:published_time\" content=\"2023-09-28T05:33:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-09-28T05:33:02+00:00\" \/>\n<meta name=\"author\" content=\"Amit Kumar\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@https:\/\/twitter.com\/amits_thakurs\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Amit Kumar\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"1 minute\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/\",\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/\",\"name\":\"What is constructor ? Use of Constructor in laravel ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#website\"},\"datePublished\":\"2023-09-28T05:33:00+00:00\",\"dateModified\":\"2023-09-28T05:33:02+00:00\",\"author\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.devopsfreelancer.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What is constructor ? Use of Constructor in laravel ?\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#website\",\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/\",\"name\":\"DevOps Freelancer\",\"description\":\"We provide DevOps | SRE | DevSecOps | MLOps Freelancing\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b\",\"name\":\"Amit Kumar\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"caption\":\"Amit Kumar\"},\"description\":\"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.\",\"sameAs\":[\"https:\/\/www.facebook.com\/amitsthakurs\/\",\"https:\/\/www.instagram.com\/amits_thakurs\/\",\"https:\/\/www.linkedin.com\/in\/amits-thakurs\/\",\"https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs\"],\"url\":\"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"What is constructor ? Use of Constructor in laravel ? - DevOps Freelancer","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"What is constructor ? Use of Constructor in laravel ? - DevOps Freelancer","og_description":"A constructor is a special method that is called automatically when an object of a class is created. It is used to initialize the object&#8217;s properties and...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-09-28T05:33:00+00:00","article_modified_time":"2023-09-28T05:33:02+00:00","author":"Amit Kumar","twitter_card":"summary_large_image","twitter_creator":"@https:\/\/twitter.com\/amits_thakurs","twitter_misc":{"Written by":"Amit Kumar","Est. reading time":"1 minute"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/","name":"What is constructor ? Use of Constructor in laravel ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-09-28T05:33:00+00:00","dateModified":"2023-09-28T05:33:02+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-is-constructor-use-of-constructor-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What is constructor ? Use of Constructor in laravel ?"}]},{"@type":"WebSite","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website","url":"https:\/\/www.devopsfreelancer.com\/blog\/","name":"DevOps Freelancer","description":"We provide DevOps | SRE | DevSecOps | MLOps Freelancing","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.devopsfreelancer.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b","name":"Amit Kumar","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","caption":"Amit Kumar"},"description":"Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer\/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.","sameAs":["https:\/\/www.facebook.com\/amitsthakurs\/","https:\/\/www.instagram.com\/amits_thakurs\/","https:\/\/www.linkedin.com\/in\/amits-thakurs\/","https:\/\/x.com\/https:\/\/twitter.com\/amits_thakurs"],"url":"https:\/\/www.devopsfreelancer.com\/blog\/author\/amit\/"}]}},"_links":{"self":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1064","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/comments?post=1064"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1064\/revisions"}],"predecessor-version":[{"id":1065,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1064\/revisions\/1065"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1064"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1064"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1064"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}