{"id":1344,"date":"2023-10-18T14:26:20","date_gmt":"2023-10-18T14:26:20","guid":{"rendered":"https:\/\/www.devopsfreelancer.com\/blog\/?p=1344"},"modified":"2023-10-31T14:57:31","modified_gmt":"2023-10-31T14:57:31","slug":"what-are-the-example-of-oops-in-laravel","status":"publish","type":"post","link":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/","title":{"rendered":"What are the example of Oops in Laravel ?"},"content":{"rendered":"\n<h2 class=\"wp-block-heading\"><strong>What is Oops ?<\/strong><\/h2>\n\n\n\n<p>&#8220;OOP&#8221; stands for Object-Oriented Programming. It&#8217;s a programming paradigm that uses objects \u2013 which are instances of classes \u2013 to organize code. The key principles of OOP are:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Encapsulation:<\/strong> Bundling of data (attributes) and the methods (functions) that operate on the data into a single unit called a class. It restricts direct access to some of the object&#8217;s components and can prevent the accidental modification of data.<\/li>\n\n\n\n<li><strong>Inheritance:<\/strong> A mechanism where a new class inherits properties and behaviors from an existing class. It promotes code reusability and establishes a relationship between classes.<\/li>\n\n\n\n<li><strong>Polymorphism:<\/strong> Allows objects to be treated as instances of their parent class, so a single interface can be used to represent different types of objects. It includes method overloading and method overriding.<\/li>\n\n\n\n<li><strong>Abstraction:<\/strong> Simplifying complex systems by modeling classes based on the essential properties and behaviors they possess. It involves creating a blueprint of a class that can be instantiated.<\/li>\n<\/ol>\n\n\n\n<p>OOP provides a way to structure a program by organizing code into objects that can interact with each other. It aims to improve code readability, reusability, and maintainability. Many modern programming languages, including PHP, Java, Python, and C++, support OOP concepts.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Example :- <\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;?php\r\n\r\nnamespace App\\Models;\r\n\r\nclass User extends Model\r\n{\r\n    protected $fillable = &#91;\r\n        'name',\r\n        'email',\r\n        'password',\r\n    ];\r\n\r\n    public function posts()\r\n    {\r\n        return $this->hasMany(Post::class);\r\n    }\r\n}<\/code><\/pre>\n\n\n\n<p>In this example, the <code class=\"\">User<\/code> class extends the <code class=\"\">Model<\/code> class, which is the base class for all Laravel models. The <code class=\"\">fillable<\/code> property specifies which attributes can be mass assigned to the model.<\/p>\n\n\n\n<p>The <code class=\"\">posts()<\/code> method defines a one-to-many relationship between the <code class=\"\">User<\/code> model and the <code class=\"\">Post<\/code> model. This means that a user can have many posts, but a post can only have one user.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2nd example :- <\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>$user = User::create(&#91;\r\n    'name' => 'John Doe',\r\n    'email' => 'john.doe@example.com',\r\n    'password' => 'password',\r\n]);\r\n\r\n\/\/ Get the user's posts\r\n$posts = $user->posts;\r\n\r\n\/\/ Create a new post for the user\r\n$post = new Post(&#91;\r\n    'title' => 'My first post',\r\n    'content' => 'This is my first post.',\r\n]);\r\n$user->posts()->save($post);\r\n<\/code><\/pre>\n\n\n\n<p>OOP is a powerful programming paradigm that can be used to write more reusable, maintainable, and testable code. Laravel makes it easy to use OOP in your PHP applications.<\/p>\n\n\n\n<p>Here are some other examples of OOP in Laravel:<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>Controllers:&nbsp;Laravel controllers are responsible for handling HTTP requests and returning responses.&nbsp;Controllers can use OOP principles to encapsulate the logic for handling specific requests.<\/li>\n\n\n\n<li>Models:&nbsp;Laravel models represent the data in your database.&nbsp;Models can use OOP principles to encapsulate the logic for interacting with the database.<\/li>\n\n\n\n<li>Services:&nbsp;Laravel services are reusable classes that can be used to perform specific tasks.&nbsp;Services can use OOP principles to encapsulate the logic for performing those tasks.<\/li>\n<\/ol>\n\n\n\n<p>OOP is a fundamental concept of Laravel, and it is used throughout the framework. By understanding OOP, you can write better Laravel code that is more reusable, maintainable, and testable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Oops ? &#8220;OOP&#8221; stands for Object-Oriented Programming. It&#8217;s a programming paradigm that uses objects \u2013 which are instances of classes \u2013 to organize code. The&#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":[525,528,3,526],"class_list":["post-1344","post","type-post","status-publish","format-standard","hentry","category-laravel","tag-encapsulation","tag-inheritance","tag-laravel","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>What are the example of Oops 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-are-the-example-of-oops-in-laravel\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"What are the example of Oops in Laravel ? - DevOps Freelancer\" \/>\n<meta property=\"og:description\" content=\"What is Oops ? &#8220;OOP&#8221; stands for Object-Oriented Programming. It&#8217;s a programming paradigm that uses objects \u2013 which are instances of classes \u2013 to organize code. The...\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-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-10-18T14:26:20+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-10-31T14:57:31+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=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/\"},\"author\":{\"name\":\"Amit Kumar\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"headline\":\"What are the example of Oops in Laravel ?\",\"datePublished\":\"2023-10-18T14:26:20+00:00\",\"dateModified\":\"2023-10-31T14:57:31+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/\"},\"wordCount\":416,\"commentCount\":0,\"keywords\":[\"encapsulation\",\"Inheritance\",\"laravel\",\"oops-in-laravel\"],\"articleSection\":[\"Laravel\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/\",\"url\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/\",\"name\":\"What are the example of Oops in Laravel ? - DevOps Freelancer\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#website\"},\"datePublished\":\"2023-10-18T14:26:20+00:00\",\"dateModified\":\"2023-10-31T14:57:31+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/#\\\/schema\\\/person\\\/22ed4bd82dc04200a2ca541b3e35fc5b\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/what-are-the-example-of-oops-in-laravel\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.devopsfreelancer.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"What are the example of Oops 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:\\\/\\\/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":"What are the example of Oops 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-are-the-example-of-oops-in-laravel\/","og_locale":"en_US","og_type":"article","og_title":"What are the example of Oops in Laravel ? - DevOps Freelancer","og_description":"What is Oops ? &#8220;OOP&#8221; stands for Object-Oriented Programming. It&#8217;s a programming paradigm that uses objects \u2013 which are instances of classes \u2013 to organize code. The...","og_url":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/","og_site_name":"DevOps Freelancer","article_author":"https:\/\/www.facebook.com\/amitsthakurs\/","article_published_time":"2023-10-18T14:26:20+00:00","article_modified_time":"2023-10-31T14:57:31+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":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/#article","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/"},"author":{"name":"Amit Kumar","@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"headline":"What are the example of Oops in Laravel ?","datePublished":"2023-10-18T14:26:20+00:00","dateModified":"2023-10-31T14:57:31+00:00","mainEntityOfPage":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/"},"wordCount":416,"commentCount":0,"keywords":["encapsulation","Inheritance","laravel","oops-in-laravel"],"articleSection":["Laravel"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/","url":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/","name":"What are the example of Oops in Laravel ? - DevOps Freelancer","isPartOf":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#website"},"datePublished":"2023-10-18T14:26:20+00:00","dateModified":"2023-10-31T14:57:31+00:00","author":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/#\/schema\/person\/22ed4bd82dc04200a2ca541b3e35fc5b"},"breadcrumb":{"@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.devopsfreelancer.com\/blog\/what-are-the-example-of-oops-in-laravel\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.devopsfreelancer.com\/blog\/"},{"@type":"ListItem","position":2,"name":"What are the example of Oops 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:\/\/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\/1344","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=1344"}],"version-history":[{"count":1,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1344\/revisions"}],"predecessor-version":[{"id":1345,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/posts\/1344\/revisions\/1345"}],"wp:attachment":[{"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/media?parent=1344"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/categories?post=1344"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.devopsfreelancer.com\/blog\/wp-json\/wp\/v2\/tags?post=1344"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}