• Call Us: +92-333-7276335
  • - Mail Us: info@shekztech.com

Plot 1177, Sector 31B - Crossing, Karachi, Sindh

Loading...
How to to create the relationship between models in laravel 10
  • Views: 827
  • Category: Laravel
  • Published at: 31 Aug, 2023
  • Updated at: 01 Sep, 2023

How to to create the relationship between models in laravel 10

Web development has evolved tremendously, and Laravel remains at the forefront, especially when it comes to database interaction and object-relational mapping (ORM). Laravel 10 continues the legacy, offering robust features and tools to create complex yet manageable database relations with ease. One such feature is Laravel's Eloquent ORM, which simplifies the process of setting up relationships between database tables via models. In this blog post, we'll delve into creating relationships between three particular models—ShekzUser, ShekzBlog, and ShekzCategory—in a Laravel 10 application.

By defining relationships between these models, you can simplify complex operations and make the codebase more readable. We'll walk you through the step-by-step instructions on how to set these up and provide examples of the artisan commands needed to generate each model.

Step 1: Understanding the Models

Before we jump into creating relationships, it's important to understand what each model represents:

ShekzUser: Represents the users in your application.
ShekzBlog: Stands for individual blog posts created by users.
ShekzCategory: Represents the category to which a blog post belongs.
Artisan Commands for Models
To create each model, you would typically run the following artisan commands:

ShekzUser: This model is often generated by Laravel's built-in auth scaffolding, but if you need to create it manually:

php artisan make:model ShekzUser

ShekzBlog:

php artisan make:model ShekzBlog

ShekzCategory:

php artisan make:model ShekzCategory

Step 2: One-to-Many Relationship Between ShekzUser and ShekzBlog

In the ShekzUser model, a user can have multiple blogs, representing a "One-to-Many" relationship.

Open your ShekzUser model.
Define the blogs() method using the hasMany() Eloquent method:

public function blogs()
{
    return $this->hasMany(ShekzBlog::class);
}

Step 3: Many-to-One Relationship Between ShekzBlog and ShekzUser/ShekzCategory

In the ShekzBlog model, each blog post belongs to a single user and a single category, representing a "Many-to-One" relationship.

Open your ShekzBlog model.
For the user, use belongsTo():

public function user()
{
    return $this->belongsTo(ShekzUser::class);
}

For the category, use belongsTo() as well:

public function category()
{
    return $this->belongsTo(ShekzCategory::class);
}

Step 4: One-to-Many Relationship Between ShekzCategory and ShekzBlog

In the ShekzCategory model, a category can have multiple blogs but belongs to a single user, representing a "One-to-Many" relationship.

Open your ShekzCategory model.
Define the following methods:

public function blogs()
{
    return $this->hasMany(ShekzBlog::class);
}

public function user()
{
    return $this->belongsTo(ShekzUser::class);
}

 

Important Note on Primary Keys and Foreign Keys

While working with Laravel's Eloquent, there are some conventions and underlying assumptions that are good to know. Eloquent assumes that each table has a primary key column named id. This makes it easier to relate tables to each other. For example, in our ShekzUser, ShekzBlog, and ShekzCategory tables, we assumed that each has an id field that serves as the primary key.

Eloquent also assumes that the foreign key on the related model is the name of the "owning" model in snake case suffixed with _id. In simpler terms, if you have a ShekzUser model, the corresponding foreign key on the ShekzBlog and ShekzCategory tables should be user_id.

 

// Example with custom primary key and foreign key
public function blogs()
{
    return $this->hasMany(ShekzBlog::class, 'custom_user_id', 'custom_id');
}

In this example, custom_user_id is the custom foreign key in the ShekzBlog model, and custom_id is the custom primary key in the ShekzUser model.

Conclusion

Understanding how to create relationships between models is a cornerstone in mastering Laravel 10 development. By utilizing Eloquent's easy-to-implement methods like hasMany() and belongsTo(), you can effectively connect models like ShekzUser, ShekzBlog, and ShekzCategory. With this knowledge, you're well-equipped to build scalable and maintainable Laravel applications. Happy coding!

Shehzad Ahmed

Shehzad Ahmed is a highly qualified expert with a Master of Philosophy in Computer Science and a decade of extensive industry experience. With his impressive track record in web development and computer science, he has not only left an indelible mark on the industry but also made substantial contributions to education. Since , he has created more than eighty exhaustive courses, paving the way for innumerable individuals interested in learning and development. His unparalleled knowledge and innate ability to elucidate complex ideas make him a highly sought-after educator and consultant. Choose Shehzad and take advantage of his exceptional balance of technical expertise and teaching prowess to propel your learning journey or project to new heights.

0 Comment(s)
Write your comment