{"id":1588,"date":"2024-01-01T05:28:15","date_gmt":"2024-01-01T05:28:15","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1588"},"modified":"2024-01-17T05:38:24","modified_gmt":"2024-01-17T05:38:24","slug":"definition-of-traits-in-laravel-with-example","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/","title":{"rendered":"Definition of Traits in Laravel with Example ?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\">What is Traits in Laravel ?<\/h2>\n\n\n\n<p>Traits is a simply a group of methods that you want include within another class. You can easily reuse that methods to another class. Trait is save to write same code again and again. <\/p>\n\n\n\n<p>In this tutorial we &#8216;have to create a trait for image upload functionality is a great way to ensure code reusability. You can use the same trait in multiple models or controllers where image uploads are required, like for profiles, product pictures, or any other entities requiring image handling.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\" id=\"9c61\">Here is the introduction to Laravel Traits in simple words:<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Traits are a way to reuse code in Laravel.<\/li>\n\n\n\n<li>They are similar to classes, but they cannot be instantiated on their own.<\/li>\n\n\n\n<li>Traits can be included in classes to give them additional methods and functionality.<\/li>\n\n\n\n<li>Traits offer several advantages, including code reusability, flexibility in composition, and adherence to the Single Responsibility Principle.<\/li>\n\n\n\n<li>Using traits can help you to organize your code, promote code reuse, simplify code maintenance, and facilitate code sharing within a team.<\/li>\n<\/ul>\n\n\n\n<p>We will create new trait with verifyAndUpload(). verifyAndUpload() helps to upload image from controller. So let&#8217;s create bellow file and write code as like bellow code:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>app\/Traits\/ImageTrait.php\r<\/code><\/pre>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n  \r\nnamespace App\\Traits;\r\n  \r\nuse Illuminate\\Http\\Request;\r\n  \r\ntrait ImageTrait {\r\n  \r\n    \/**\r\n     * @param Request $request\r\n     * @return $this|false|string\r\n     *\/\r\n    public function verifyAndUpload(Request $request, $fieldname = 'image', $directory = 'images' ) {\r\n  \r\n        if( $request->hasFile( $fieldname ) ) {\r\n  \r\n            if (!$request->file($fieldname)->isValid()) {\r\n  \r\n                flash('Invalid Image!')->error()->important();\r\n  \r\n                return redirect()->back()->withInput();\r\n  \r\n            }\r\n \r\n            return $request->file($fieldname)->store($directory, 'public');\r\n  \r\n        }\r\n\r\n        return null;\r\n\r\n    }\r\n  \r\n}<\/code><\/pre>\n\n\n\n<p>So, let&#8217;s write code as bellow for controller.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><strong>app\/Http\/Controllers\/ItemController.php<\/strong><\/code><\/pre>\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\nuse App\\Item;\r\nuse App\\Traits\\ImageTrait;\r\n  \r\nclass ItemController extends Controller\r\n{\r\n    use ImageTrait;\r\n     \/**\r\n     * Display a listing of the resource.\r\n     *\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function create()\r\n    {\r\n        return view('imageUpload');\r\n    }\r\n   \r\n    \/**\r\n     * Display a listing of the resource.\r\n     *\r\n     * @return \\Illuminate\\Http\\Response\r\n     *\/\r\n    public function store(Request $request)\r\n    {\r\n        $input = $request->all();\r\n   \r\n        $input&#91;'image'] = $this->verifyAndUpload($request, 'image', 'images');\r\n   \r\n        Item::create($input);\r\n    \r\n        return back()\r\n            ->with('success','record created successfully.');\r\n   \r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>Now you can use traits &#8230;&#8230;&#8230;&#8230;..<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Traits in Laravel ? Traits is a simply a group of methods that you want include within another class. You can easily reuse that methods&#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":[3,621,620,526],"class_list":["post-1588","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-laravel","tag-laravel-image-traits","tag-laravel-traits","tag-oops-in-laravel"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Definition of Traits in Laravel with Example ? - 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\/definition-of-traits-in-laravel-with-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Definition of Traits in Laravel with Example ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"What is Traits in Laravel ? Traits is a simply a group of methods that you want include within another class. You can easily reuse that methods...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/\" \/>\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=\"2024-01-01T05:28:15+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-01-17T05:38:24+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\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"Definition of Traits in Laravel with Example ?\",\"datePublished\":\"2024-01-01T05:28:15+00:00\",\"dateModified\":\"2024-01-17T05:38:24+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/\"},\"wordCount\":220,\"commentCount\":0,\"keywords\":[\"laravel\",\"laravel-image-traits\",\"laravel-traits\",\"oops-in-laravel\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/\",\"name\":\"Definition of Traits in Laravel with Example ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"datePublished\":\"2024-01-01T05:28:15+00:00\",\"dateModified\":\"2024-01-17T05:38:24+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/definition-of-traits-in-laravel-with-example\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Definition of Traits in Laravel with Example ?\"}]},{\"@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:\\\/\\\/secure.gravatar.com\\\/avatar\\\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g\",\"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":"Definition of Traits in Laravel with Example ? - 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\/definition-of-traits-in-laravel-with-example\/","og_locale":"en_US","og_type":"article","og_title":"Definition of Traits in Laravel with Example ? - DevOps Freelancer","og_description":"What is Traits in Laravel ? Traits is a simply a group of methods that you want include within another class. You can easily reuse that methods...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2024-01-01T05:28:15+00:00","article_modified_time":"2024-01-17T05:38:24+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":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"Definition of Traits in Laravel with Example ?","datePublished":"2024-01-01T05:28:15+00:00","dateModified":"2024-01-17T05:38:24+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/"},"wordCount":220,"commentCount":0,"keywords":["laravel","laravel-image-traits","laravel-traits","oops-in-laravel"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/","name":"Definition of Traits in Laravel with Example ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2024-01-01T05:28:15+00:00","dateModified":"2024-01-17T05:38:24+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/definition-of-traits-in-laravel-with-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Definition of Traits in Laravel with Example ?"}]},{"@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:\/\/secure.gravatar.com\/avatar\/d76fb4d0f15f7a458f1fd91063b44fbb7e7eb9e724b1c465d885054c2540250f?s=96&d=mm&r=g","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\/1588","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=1588"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1588\/revisions"}],"predecessor-version":[{"id":1589,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1588\/revisions\/1589"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1588"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1588"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1588"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}