What are the example of Inheritance laravel

What is Inheritance ?

Inheritance is a fundamental concept in object-oriented programming (OOP) that allows a new class (subclass or derived class) to inherit the characteristics and behaviors of an existing class (superclass or base class). This means that the subclass can reuse the code of the superclass, promoting code reusability and establishing a hierarchical relationship between classes.

What are the benefits of Inheritance in laravel ?

In Laravel, as in any object-oriented programming (OOP) framework, the benefits of inheritance remain consistent. Here are some advantages specific to Laravel:

  1. Code Reusability: Inheritance allows you to reuse code from existing classes in new classes. In Laravel, this means that you can extend and inherit functionality from base classes, reducing the need to duplicate code.
  2. Maintainability: When you make changes to a base class, those changes automatically propagate to all the subclasses that inherit from it. This makes it easier to maintain and update your codebase.
  3. Polymorphism: Inheritance supports polymorphism, which allows objects of different classes to be treated as objects of a common base class. This can be particularly useful in Laravel when dealing with relationships and polymorphic relationships in Eloquent models.
  4. Organized Code Structure: By using inheritance, you can create a clear and organized class hierarchy. This makes your code more readable and understandable, especially for developers who are new to the project.
  5. Framework Extensibility: Laravel itself extensively uses inheritance to provide a flexible and extensible framework. Many of the core components, such as controllers and models, can be extended to customize their behavior according to your application’s needs.

Example :-


namespace App\Models;

class User extends Model
    protected $fillable = [

    public function posts()
        return $this->hasMany(Post::class);

class Admin extends User
    // Add new properties
    protected $fillable = [

    // Override the canPost() method from the parent class
    public function canPost()
        return true;

In this example, the Admin class inherits from the User class. This means that the Admin class has access to all of the properties and methods of the User class, including the fillable property and the posts() method.

The Admin class also adds new properties and overrides the canPost() method. This allows the Admin class to be more specialized for representing administrator data and functionality.

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.

Related Posts

How to Crawl any website Meta Title and Meta Description in Laravel ?

1st step install below package. Next to create Controller First go to route and put below code Next go to controller and put below code Next go…

SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into `oauth_clients` (`user_id`, `name`, `secret`

In this tutorial i’m going to solve the error SQLSTATE[HY000] [2002] No such file or directory (Connection: mysql, SQL: insert into oauth_clients (user_id, name, secret Error :-…

Top 20 Laravel Interview Question in 2024

In this tutorial im going share interview experience For laravel developer. A list of top frequently asked Laravel Interview Questions and answers are given below. Q #1) What is…

How to Get Google Analytics API key ?

In this tutorial we’re going to share how to get the google Analytics API key. I have shared in very easy way. First go enable Google analytics…

Youtube Subscriber Count in ReactJs

In this tutorial i’m going to learn how to count YouTube Subsribers count, views count as well, as define below. In order to install your app, first…

How to Disable Laravel’s Eloquent timestamps in laravel ?

In this tutorial we’re going to share how to disable the automatic created_at and updated_at timestamps in Laravel’s Eloquent models, along with explanations of different scenarios: 1st…

0 0 votes
Article Rating
Notify of
1 Comment
Newest Most Voted
Inline Feedbacks
View all comments

[…] What are the example of Inheritance laravel […]

Would love your thoughts, please comment.x