Académique Documents
Professionnel Documents
Culture Documents
Software Engineer Laravel Expert Docker Enthusiast Open Source Advocate (https://zalt.me)
Oct 2 6 min read
Business Rules:
The Owner can own one Car.
The Car can be owned by one Owner.
Relations Diagram:
Relationship Details:
The Cars table should store the Owner ID.
Eloquent Models:
class Owner
{
public function car()
{
return $this->hasOne(Car::class);
}
}
class Car
{
public function owner()
{
return $this->belongsTo(Owner::class);
}
}
Database Migrations:
$table->integer('owner_id')->unsigned()->index()-
>nullable();
$table->foreign('owner_id')->references('id')-
>on('owners');
});
Store Records:
$owner->car()->save($car);
$car->owner()->associate($owner)->save();
Retrieve Records:
$owner->car;
$car->owner;
One to Many Relationship
Demo details:
In this demo we have 2 models (Thief and Car), and 2 tables (thieves
and cars).
Business Rules:
The Thief can steal many Cars.
The Car can be stolen by one Thief.
Relations Diagram:
Relationship Details:
The Cars table should store the Thief ID.
Eloquent Models:
class Thief
{
public function cars()
{
return $this->hasMany(Car::class);
}
}
class Car
{
public function thief()
{
return $this->belongsTo(Thief::class);
}
}
Database Migrations:
$table->integer('thief_id')->unsigned()->index()-
>nullable();
$table->foreign('thief_id')->references('id')-
>on('thieves');
});
Store Records:
$thief->cars()->saveMany([
$car1,
$car2,
]);
$thief->cars()->save($car);
$car->thief()->associate($thief)->save();
Retrieve Records:
// Get Thief Car
$thief->cars;
$car->thief;
Business Rules:
The Man (buyer) can buy many Cars.
The Woman (buyer) can buy many Cars.
The Car can be bought by one buyer (Man or Woman).
Relations Diagram:
Relationship Details:
The Car table should store the Buyer ID and the Buyer Type.
buyer is a name given to a group of models (Man and Woman). And its
not limited to two. The buyer type is the real name of the model.
Eloquent Models:
class Man
{
public function cars()
{
return $this->morphMany(Car::class, 'buyer');
}
}
class Woman
{
public function cars()
{
return $this->morphMany(Car::class, 'buyer');
}
}
class Car
{
public function buyer()
{
return $this->morphTo();
}
}
Database Migrations:
$table->integer('buyer_id')->unsigned()->index()-
>nullable();
$table->string('buyer_type')->nullable();
});
Store Records:
$man->cars()->saveMany([
$car1,
$car2,
]);
$woman->cars()->saveMany([
$car1,
$car2,
]);
$man->cars()->save($car);
$woman->cars()->save($car);
$car1->buyer()->associate($man)->save();
$car2->buyer()->associate($woman)->save();
Retrieve Records:
$men->cars
$women->cars
$car->buyer
Business Rules:
The Driver can drive many Cars.
The Car can be driven by many Drivers.
Relations Diagram:
Relationship Details:
The Pivot table car_driver should store the Driver ID and the Car ID.
Eloquent Models:
class Driver
{
public function cars()
{
return $this->belongsToMany(Car::class);
}
}
class Car
{
public function drivers()
{
return $this->belongsToMany(Driver::class);
}
}
Database Migrations:
$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');
});
Store Records:
$driver->cars()->attach([
$car1->id,
$car2->id,
]);
$driver->cars()->sync([
$car1->id,
$car2->id,
]);
$car->drivers()->attach([
$driver1->id,
$driver2->id,
]);
$car->drivers()->sync([
$driver1->id,
$driver2->id,
]);
Retrieve Records:
$driver->cars
$car->drivers
Business Rules:
The Valet (driver) can drive many Cars.
The Owner (driver) can drive many Cars.
The Car can be driven by many drivers (Valet or/and Owner).
Relations Diagram:
Relationship Details:
The Pivot table drivers should store the Driver ID, Driver Type and
the Car ID.
driver is a name given to a group of models (Valet and Owner). And its
not limited to two. The driver type is the real name of the model.
Eloquent Models:
class Valet
{
public function cars()
{
return $this->morphToMany(Car::class, 'driver');
}
}
class Owner
{
public function cars()
{
return $this->morphToMany(Car::class, 'driver');
}
}
class Car
{
public function valets()
{
return $this->morphedByMany(Man::class, 'driver');
}
public function owners()
{
return $this->morphedByMany(Woman::class, 'driver');
}
}
Database Migrations:
$table->integer('driver_id')->unsigned()->index();
$table->integer('driver_type');
$table->integer('car_id')->unsigned()->index();
$table->foreign('car_id')->references('id')->on('cars')-
>onDelete('cascade');
});
Store Records:
$valet->cars()->saveMany([$car1, $car2]);
$owner->cars()->saveMany([$car1, $car2]);
$valet->cars()->save($car1);
$owner->cars()->save($car1);
$car->valets()->attach([
$valet1->id,
$valet2->id,
]);
$car->owners()->attach([
$owner1->id,
$owner2->id,
]);
$car->valets()->sync([
$valet1->id,
$valet2->id,
]);
$car->owners()->sync([
$owner1->id,
$owner2->id,
]);
Retrieve Records:
$valet->cars
$owner->cars
$car->owners
$car->valets
. . .