Laravel

Laravel Table Relationship Methods With Example

This Laravel tutorial helps to understand table Relationships using Elequonte ORM. We’ll explore laravel table Relationships usage and best practices with examples.

Understanding Laravel Table Relationships

There are the following types of table relationships supported by Laravel:

  • One-to-One
  • One-to-Many
  • Many-to-One (Inverse of One-to-Many)
  • Many-to-Many

Let’s assume you have an “employees” and a “departments” table, and you want to perform a join operation using Laravel’s Eloquent ORM. Define the relationship between Employee and Department models:

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Employee extends Model
{
    public function department()
    {
        return $this->belongsTo(Department::class);
    }
}

class Department extends Model
{
    public function employees()
    {
        return $this->hasMany(Employee::class);
    }
}

One-to-One Relationship

This relationship associated each record in one table with exactly one record in another table.

Related Post
class Employee extends Model
{
    public function department()
    {
        return $this->hasOne(Department::class);
    }
}

class Department extends Model
{
    public function employee()
    {
        return $this->belongsTo(Employee::class);
    }
}

One-to-Many Relationship:

A one-to-many relationship signifies that each record in one table can be associated with multiple records in another table.

class Department extends Model
{
    public function employees()
    {
        return $this->hasMany(Employee::class);
    }
}

class Employee extends Model
{
    public function department()
    {
        return $this->belongsTo(Department::class);
    }
}

Many-to-Many Relationship:

A many-to-many relationship associates each record in one table with multiple records in another table, and vice versa.

class Employee extends Model
{
    public function departments()
    {
        return $this->belongsToMany(Department::class);
    }
}

class Department extends Model
{
    public function employees()
    {
        return $this->belongsToMany(Employee::class);
    }
}

Employees may belong to multiple departments, and departments may have multiple employees.

Conclusion

The table relationship methods provide a powerful mechanism for managing data associations between database tables. You can define different associations between tables and use the power of Laravel’s Eloquent ORM.

Recent Posts

What is the Purpose of php_eol in PHP?

in this quick PHP tutorial, We'll discuss php_eol with examples. PHP_EOL is a predefined constant in PHP and represents an… Read More

2 months ago

Exploring the Power of Laravel Eloquent Join?

We'll explore different join methods of Laravel eloquent with examples. The join helps to fetch the data from multiple database… Read More

3 months ago

Quick and Easy Installation of Laravel Valet

in this Laravel tutorial, We'll explore valet, which is a development environment for macOS minimalists. It's a lightweight Laravel development… Read More

3 months ago

What is Laravel Soft Delete and How Does it Work?

I'll go through how to use soft delete in Laravel 10 in this post. The soft deletes are a method… Read More

3 months ago

Common Practices for Laravel Blade Template

in this Laravel tutorial, I will explore common practices for using the Laravel Blade template with examples. Blade is a… Read More

4 months ago

Laravel firstOrNew, firstOrCreate, firstOr, updateOrCreate With Example

in this article, we'll discuss some useful methods of Laravel eloquent model class. The methods are: firstOrNew(), firstOrCreate(), firstOr(), and… Read More

4 months ago

Categories