{"id":6300,"date":"2022-12-23T04:47:56","date_gmt":"2022-12-23T04:47:56","guid":{"rendered":"https:\/\/studysection.com\/blog\/?p=6300"},"modified":"2022-12-23T07:17:36","modified_gmt":"2022-12-23T07:17:36","slug":"how-to-use-the-service-layer-in-laravel","status":"publish","type":"post","link":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/","title":{"rendered":"How to use the Service Layer in Laravel"},"content":{"rendered":"<p>Every developer tries to maintain their code properly. They want to make their code more reusable and keep it clean. This can only be possible when they divide their code into different layers. The service layer in Laravel is the critical layer for making your business logic reusable.<\/p>\n<p>As we know, controllers should only handle input. We usually keep logic in the models, but after some time, they get &#8220;fat&#8221;. At this point, we can take advantage of the service layer in Laravel.<\/p>\n<h2>What can we do in this layer:<\/h2>\n<ol>\n<li>Implement Validation Checks<\/li>\n<li>Handle CRUDS<\/li>\n<li>Make customization in the result returned by Model<\/li>\n<li>Send Mails<\/li>\n<\/ol>\n<p><em>When you want to create a service class in Laravel, just go to the app\/services folder. Then create a class like I am creating a user class in the image below:<\/em><br \/>\n<code>&lt;?php<br \/>\nnamespace App\\Services;<br \/>\nuse Validator;<br \/>\nuse Mail;<br \/>\nuse App\\Models\\User as UserModel;<br \/>\nclass User<br \/>\n{<br \/>\n\/** Get a validator for a user. *********\/<br \/>\npublic function validator(array $data)<br \/>\n{<br \/>\nreturn Validator::make($data, [<br \/>\n'email'     =&gt; 'required|email|max:255',<br \/>\n'phone'     =&gt; 'max:255',<br \/>\n'first_name' =&gt; 'required|max:255',<br \/>\n'last_name'  =&gt; 'required|max:255'<br \/>\n]);<br \/>\n}<br \/>\n\/** Create a new user instance after a valid form. *\/<br \/>\npublic function create(array $data)<br \/>\n{<br \/>\n$data = [<br \/>\n'email'     =&gt; $data['email'],<br \/>\n'firstName' =&gt; $data['firstName'],<br \/>\n'lastName'  =&gt; $data['lastName'],<br \/>\n'language'  =&gt; $data['language'],<br \/>\n'phone'     =&gt; $data['phone'],<br \/>\n'message'   =&gt; $data['message']\n];<br \/>\n\/\/ You can also write the email code here to send it to the newly created User<br \/>\nreturn UserModel::create($data);<br \/>\n}<br \/>\n}<\/code><\/p>\n<p>This is my Controller where I am calling my Service Class in the user() method:<\/p>\n<p><code>&lt;?php<br \/>\nnamespace App\\Http\\Controllers;<br \/>\nuse App\\Services\\User as UserService;<br \/>\nuse Illuminate\\Http\\Request;<br \/>\nuse Illuminate\\Support\\Facades\\Lang;<br \/>\nclass UsersController extends Controller<br \/>\n{<br \/>\npublic function __construct(Request $request)<br \/>\n{<br \/>\n$this-&gt;_request  = $request;<br \/>\n}<br \/>\npublic function user(UserService $userService)<br \/>\n{<br \/>\n$errors  = null;<br \/>\n$success = false;<br \/>\nif ($this-&gt;_request-&gt;isMethod('post')) {<br \/>\n$validator = $userService-&gt;validator($this-&gt;_request-&gt;all());<br \/>\nif ($validator-&gt;fails()) {<br \/>\n$errors = $validator-&gt;errors();\/\/ Returns errors<br \/>\n} else {<br \/>\n$userService-&gt;create($validator-&gt;getData());<br \/>\n$success = true;<br \/>\n}<br \/>\n}<br \/>\nreturn view('pages\/user, ['errors' =&gt; $errors, 'success' =&gt; $success]); \/\/ Redirect to the User view page<br \/>\n}<br \/>\n}<\/code><\/p>\n<p><small>If you have knowledge of financial accounting and you are in search of a good job, financial accounting certification can help you reach your desired goals. StudySection provides <a href=\"https:\/\/www.studysection.com\/financial-accounting-foundation\">Financial accounting certification <\/a>for beginner level as well as expert level people in the commerce stream. You can appear in the certification exam for free to get certified.<\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Every developer tries to maintain their code properly. They want to make their code more reusable and keep it clean.<\/p>\n","protected":false},"author":1,"featured_media":6301,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[65,305],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v21.7 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>How to use the Service Layer in Laravel - SS Blog<\/title>\n<meta name=\"description\" content=\"Every developer tries to maintain their code properly. When you want to create a Service class\/Layer in Laravel, just go to the app folder.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to use the Service Layer in Laravel - SS Blog\" \/>\n<meta property=\"og:description\" content=\"Every developer tries to maintain their code properly. When you want to create a Service class\/Layer in Laravel, just go to the app folder.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\" \/>\n<meta property=\"og:site_name\" content=\"Blog Posts on famous people, innovations and educational topics\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/studysection\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-23T04:47:56+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-12-23T07:17:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/12\/Laravel.png\" \/>\n\t<meta property=\"og:image:width\" content=\"300\" \/>\n\t<meta property=\"og:image:height\" content=\"200\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"admin-studysection-blog\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@studysection\" \/>\n<meta name=\"twitter:site\" content=\"@studysection\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"admin-studysection-blog\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\"},\"author\":{\"name\":\"admin-studysection-blog\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\"},\"headline\":\"How to use the Service Layer in Laravel\",\"datePublished\":\"2022-12-23T04:47:56+00:00\",\"dateModified\":\"2022-12-23T07:17:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\"},\"wordCount\":216,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"keywords\":[\"laravel\",\"Service\"],\"articleSection\":[\"Learn and Grow\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\",\"url\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\",\"name\":\"How to use the Service Layer in Laravel - SS Blog\",\"isPartOf\":{\"@id\":\"https:\/\/studysection.com\/blog\/#website\"},\"datePublished\":\"2022-12-23T04:47:56+00:00\",\"dateModified\":\"2022-12-23T07:17:36+00:00\",\"description\":\"Every developer tries to maintain their code properly. When you want to create a Service class\/Layer in Laravel, just go to the app folder.\",\"breadcrumb\":{\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/studysection.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to use the Service Layer in Laravel\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/studysection.com\/blog\/#website\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"name\":\"Blog Posts on famous people, innovations and educational topics\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/studysection.com\/blog\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/studysection.com\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/studysection.com\/blog\/#organization\",\"name\":\"StudySection\",\"url\":\"https:\/\/studysection.com\/blog\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"contentUrl\":\"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png\",\"width\":920,\"height\":440,\"caption\":\"StudySection\"},\"image\":{\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/studysection\",\"https:\/\/twitter.com\/studysection\",\"https:\/\/www.instagram.com\/study.section\/\",\"https:\/\/www.linkedin.com\/company\/studysection\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402\",\"name\":\"admin-studysection-blog\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g\",\"caption\":\"admin-studysection-blog\"},\"url\":\"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How to use the Service Layer in Laravel - SS Blog","description":"Every developer tries to maintain their code properly. When you want to create a Service class\/Layer in Laravel, just go to the app folder.","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:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"How to use the Service Layer in Laravel - SS Blog","og_description":"Every developer tries to maintain their code properly. When you want to create a Service class\/Layer in Laravel, just go to the app folder.","og_url":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/","og_site_name":"Blog Posts on famous people, innovations and educational topics","article_publisher":"https:\/\/www.facebook.com\/studysection","article_published_time":"2022-12-23T04:47:56+00:00","article_modified_time":"2022-12-23T07:17:36+00:00","og_image":[{"width":300,"height":200,"url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2022\/12\/Laravel.png","type":"image\/png"}],"author":"admin-studysection-blog","twitter_card":"summary_large_image","twitter_creator":"@studysection","twitter_site":"@studysection","twitter_misc":{"Written by":"admin-studysection-blog","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#article","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/"},"author":{"name":"admin-studysection-blog","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402"},"headline":"How to use the Service Layer in Laravel","datePublished":"2022-12-23T04:47:56+00:00","dateModified":"2022-12-23T07:17:36+00:00","mainEntityOfPage":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/"},"wordCount":216,"commentCount":0,"publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"keywords":["laravel","Service"],"articleSection":["Learn and Grow"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/","url":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/","name":"How to use the Service Layer in Laravel - SS Blog","isPartOf":{"@id":"https:\/\/studysection.com\/blog\/#website"},"datePublished":"2022-12-23T04:47:56+00:00","dateModified":"2022-12-23T07:17:36+00:00","description":"Every developer tries to maintain their code properly. When you want to create a Service class\/Layer in Laravel, just go to the app folder.","breadcrumb":{"@id":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/studysection.com\/blog\/how-to-use-the-service-layer-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/studysection.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to use the Service Layer in Laravel"}]},{"@type":"WebSite","@id":"https:\/\/studysection.com\/blog\/#website","url":"https:\/\/studysection.com\/blog\/","name":"Blog Posts on famous people, innovations and educational topics","description":"","publisher":{"@id":"https:\/\/studysection.com\/blog\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/studysection.com\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/studysection.com\/blog\/#organization","name":"StudySection","url":"https:\/\/studysection.com\/blog\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/","url":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","contentUrl":"https:\/\/studysection.com\/blog\/wp-content\/uploads\/2021\/10\/studySection-logo.png","width":920,"height":440,"caption":"StudySection"},"image":{"@id":"https:\/\/studysection.com\/blog\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/studysection","https:\/\/twitter.com\/studysection","https:\/\/www.instagram.com\/study.section\/","https:\/\/www.linkedin.com\/company\/studysection"]},{"@type":"Person","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/db367e2c29a12d1808fb1979edb3d402","name":"admin-studysection-blog","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/studysection.com\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/054ac87a6874df1932004239cd8eab36?s=96&d=mm&r=g","caption":"admin-studysection-blog"},"url":"https:\/\/studysection.com\/blog\/author\/admin-studysection-blog\/"}]}},"views":926,"_links":{"self":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6300"}],"collection":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/comments?post=6300"}],"version-history":[{"count":3,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6300\/revisions"}],"predecessor-version":[{"id":6304,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/posts\/6300\/revisions\/6304"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media\/6301"}],"wp:attachment":[{"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/media?parent=6300"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/categories?post=6300"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/studysection.com\/blog\/wp-json\/wp\/v2\/tags?post=6300"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}