Author - StudySection Post Views - 58 views
relationships

Eloquent: Relationships

Database tables are often related to one another. For example, a student in college could have several subjects, a bank account is related to the user who owns that. Eloquent makes managing and dealing with these relationships straight-forward and supports many different types of relationships:

  1. One To One
  2. One To Many
  3. Many To Many D(2
  4. Has One Through
  5. Has Many Through
  6. One To One (Polymorphic)
  7. One To Many (Polymorphic)
  8. Many To Many (Polymorphic)

One To One

A one-to-one relationship is a very basic relation. For example, a person has only one car which belongs to its owner.
Example: We have 2 models (OWNER and CAR) and two tables (owners and cars). Car belongs to one owner or the owner has only one car.

db1 relationships

The car table should have ownerID in it.
Eloquent Model:
Owner Model:
class Owner
{
public function car()
{
return $this->hasOne(Car::class);
}
}
Car model:
class Car
{
public function owner()
{
return $this->belongsTo(Owner::class);
}
}
Database Migrations:
Owner Schema:
Schema::create('owners', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Cars Schema :
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); $table->integer('owner_id')->unsigned()->index()->nullable();
$table->foreign('owner_id')->references('id')->on('owners');
});

Additionally, eloquent assumes that the foreign key should have a worth value matching the id (or the custom $primaryKey) column of the parent. In alternative words, eloquent can seek for the value of the user’s id column within the user_id column of the Phone record. If you’d just like the relationship to use a price apart from id, you’ll pass the 3rd argument to the hasOne technique specifying your custom key:
return $this->hasOne('App\Car', 'foreign_key', 'local_key');

One To Many

A one-to-many relationship defines the relationships where a single model has any amount of other models.
Example: We have 2 models (Thief and CAR) and two tables (thieves and cars). A thief can steal many cars or a car can be stolen by one thief.

db2 relationships

The Cars table should store the thiefId.
Eloquent Model:
Thief Model :
class Thief
{
public function cars()
{
return $this->hasMany(Car::class);
}
}
Car model:
class Car
{
public function thief()
{
return $this->belongsTo(Thief::class);
}
}
Database Migrations:
Thief Schema:
Schema::create('thieves', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Cars Schema:
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->integer('thief_id')->unsigned()->index()->nullable();
$table->foreign('thief_id')->references('id')->on('thieves');
});

Like the hasOne method, you may also override the foreign and local keys by passing additional arguments to the hasMany method.

Many To Many

Many-to-many relations are slightly more complicated than hasOne and hasMany relationships. An example of such a relationship is a user with many roles, where the roles are also shared by other users.
Example: We have 2 models (Driver and Car), and 3 tables (drivers, cars and a pivot table named car_driver).

db3
The Pivot table “car_driver” should store the driverId and the carId.
Eloquent Model:
Driver Model:
class Driver
{
public function cars()
{
return $this->belongsToMany(Car::class);
}
}
Car model:
class Car
{
public function thief()
{
return $this->belongsToMany(Driver::class);
}
}
Database Migrations:
Driver Schema:
Schema::create('drivers', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Cars Schema:
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Pivot Schema:
Schema::create('car_driver', function (Blueprint $table) {
$table->increments('id');
$table->integer('car_id')->unsigned()->index();
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
$table->integer('driver_id')->unsigned()->index();
$table->foreign('driver_id')->references('id')->on('drivers')->onDelete('cascade');
});

One To Many (Polymorphic)

A polymorphic relationship makes it possible for the target model that it can belong to one or more than one type of model using a single association. A one-to-many polymorphic relation is the same as that of simple one-to-one relation. However, the target model can belong to more than one type of model on a single association. For example, we have 3 models (Man, Woman, and Car) and 3 tables (men, women, and cars).

  • The Man (buyer) can buy many Cars.
  • The Woman (buyer) can buy many Cars.
  • One buyer (Man or Woman) can buy the Car.

db4
Eloquent Model:
Man Model:
class Man
{
public function cars()
{
return $this->morphMany(Car::class, 'buyer');
}
}
Woman model:
class Woman
{
public function cars()
{
return $this->morphMany(Car::class, 'buyer');
}
}
Woman model:
class Car
{
public function buyer()
{
return $this->morphTo();
}
}
Database Migrations:
Men Schema:
Schema::create('men', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Women Schema:
Schema::create('women', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Cars Schema:
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name'); $table->integer('buyer_id')->unsigned()->index()->nullable();
$table->string('buyer_type')->nullable();
// or use $table->morphs(‘buyer’); instead of "buyer_id" and "buyer_type"
});

One To Many (Polymorphic)

A one-to-many polymorphic relation is similar to a simple one-to-many relationship. However, the target model can belong to more than one type of model on a single association. For example, we have 3 models (Valet, Owner, and Car), and 4 tables (valets, owners, cars and drivers).

  • The Valet (driver) can drive many Cars.
  • The Owner (driver) can drive many Cars.
  • There can be many drivers (Valet or/and Owner) for one Car.

db5
Eloquent Model:
Valet Model:
class Valet
{
public function cars()
{
return $this->morphToMany(Car::class, 'driver');
}
}
Owner model:
class Owner
{
public function cars()
{
return $this->morphToMany(Car::class, 'driver');
}
}
Car model:
class Car
{
public function valets()
{
return $this->morphedByMany(Valet::class, 'driver');
}

public function owners()
{
return $this->morphedByMany(Owner::class, 'driver');
}
}
Database Migrations:
Valets Schema:
Schema::create('valets', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Owners Schema:
Schema::create('owners', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
Drivers Schema:
Schema::create('drivers', function (Blueprint $table) {
$table->increments('id'); $table->integer('driver_id')->unsigned()->index();
$table->string('driver_type');
// or use $table->morphs(‘driver’); instead of "driver_id" and "driver_type" $table->integer('car_id')->unsigned()->index();
$table->foreign('car_id')->references('id')->on('cars')->onDelete('cascade');
});

Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification exam conducted by StudySection. After going through this Computer Certification Exam, you will be able to evaluate your basic knowledge of computers.

Leave a Reply

Your email address will not be published. Required fields are marked *