DATABASE RELATION CHEATSHEETdocx

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 6

One to One

Contoh kita memiliki 2 Model (Owner and Car), Rules:


dan 2 tabel (owners and cars). Owner dapat memiliki 1 Car
Car dapat dimiliki oleh 1 Owner

Eloquent Models:

class Owner
{
public function car()
{
return $this->hasOne(Car::class);
}
}
class Car
{
public function owner()
{
return $this->belongsTo(Owner::class);
}
}

Tabel Cars harus memiliki kolom Owner ID.

Database Migrations:

Schema::create('owners', function (Blueprint $table) {


$table->increments('id');
$table->string('name');
});
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');
});

Store Records: Retrieve Records:

// Create relation between Owner and Car. // Get Owner Car


$owner->car()->save($car); $owner->car;
// Create relation between Car and Owner. // Get Car Owner
$car->owner()->associate($owner)->save(); $car->owner;
One to many
Contoh kita memiliki 2 model (Thief and Car), dan Rules:
2 tabel (thieves and cars). Thief dapat mencuri banyak Cars.
Car dapat dicuri oleh satu Thief.

Eloquent Models:

class Thief
{
public function cars()
{
return $this->hasMany(Car::class);
}
}
class Car
{
public function thief()
{
return $this->belongsTo(Thief::class);
}
}

Tabel Cars harus memiliki kolom Thief ID.

Database Migrations:

Schema::create('thieves', function (Blueprint $table) {


$table->increments('id');
$table->string('name');
});
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');
});

Store Records: Retrieve Records:

// Create relation between Thief and Car. // Get Thief Car


$thief->cars()->saveMany([ $thief->cars;
$car1, // Get Car Thief
$car2, $car->thief;
]);
// Or use the save() function for single model.
$thief->cars()->save($car);
// Create relation between Car and Thief.
$car->thief()->associate($thief)->save();
Polymorphic One to Many Relationship
Contoh kita memiliki 3 model (Man, Woman Rules:
Man (pembeli) dapat membeli banyak Car.
and Car), dan 3 tabel (men, women and cars).
Women (pembeli) dapat membeli banyak Car.
Car dapat dibeli oleh satu pembeli (Man atau Women).

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()
Tabel Cars harus menyimpan buyer_id dan {
buyer_type."buyer" adalah nama yang return $this->morphTo();
diberikan kepada sekelompok model (Man dan }
Woman). Dan itu tidak terbatas pada dua. }
Buyer_Type adalah nama asli model.

Database Migrations:

Schema::create('men', function (Blueprint $table) {


$table->increments('id');
$table->string('name');
});
Schema::create('women', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
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"
});

Store Records: Retrieve Records:

/ Create relation between buyer (Man/Woman) and Car. // Get buyer (Man/Woman) Cars
$man->cars()->saveMany([ $men->cars
$car1, $women->cars
$car2, // Get Car buyer (Man and Woman)
]); $car->buyer
$woman->cars()->saveMany([
$car1,
$car2,
]);
// Or use the save() function for single model.
$man->cars()->save($car);
$woman->cars()->save($car);
// Create relation between Car and buyer (Men/Women).
$car1->buyer()->associate($man)->save();
$car2->buyer()->associate($woman)->save();
Many to Many Relationship
Contoh kita memiliki 2 model (Driver, Car), Rules:
dan 3 tabel (drivers, cars and car_driver). Driver dapat mengendarai banyak Cars.
Car dapat dikendarai oleh banyak Drivers.

Eloquent Models:

class Driver
{
public function cars()
{
return $this->belongsToMany(Car::class);
}
}
class Car
{
public function drivers()
{
return $this->belongsToMany(Driver::class);
}
}

Tabel Pivot "car_driver" harus menyimpan 'driver_id’ dan 'car_id'.

Database Migrations:

Schema::create('drivers', function (Blueprint $table) {


$table->increments('id');
$table->string('name');
});
Schema::create('cars', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
});
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');
});

Store Records: Retrieve Records:


// Create relation between Driver and Car. // Get Driver Car
$driver->cars()->attach([ $driver->cars
$car1->id, // Get Car Drivers
$car2->id, $car->drivers
]);
// Or use the sync() function to prevent duplicated
relations.
$driver->cars()->sync([
$car1->id,
$car2->id,
]);
// Create relation between Car and Driver.
$car->drivers()->attach([
$driver1->id,
$driver2->id,
]);
// Or use the sync() function to prevent duplicated
relations.
$car->drivers()->sync([
$driver1->id,
$driver2->id,
]);
Polymorphic Many to Many Relationship
Contoh kita memiliki 2 model (Valet, Rules:
Owner and Car), dan 4 tabel (valets, Valet (pengemudi) dapat mengendarai banyak Car.
owners, cars and drivers). Owner (pengemudi) dapat mengendarai banyak Car.
Car dapat dikendarai oleh banyak pengemudi (Valet atau / dan Owner ).
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 {
tabel Pivot “Drivers” harus public function valets()
menyimpan Driver ID, Driver Type {
dan Car ID. return $this->morphedByMany(Valet::class, 'driver');
"Driver" adalah nama yang diberikan }
kepada sekelompok model (Valet public function owners()
dan Owner). Dan itu tidak terbatas {
pada dua. Tipe driver adalah nama return $this->morphedByMany(Owner::class, 'driver');
asli dari model. }
}

Database Migrations: Store Records:


Schema::create('valets', function (Blueprint // Create relation between driver
$table) { (Valet/Owner) and Car.
$table->increments('id'); $valet->cars()->saveMany([$car1, $car2]);
$table->string('name'); $owner->cars()->saveMany([$car1, $car2]);
}); // Or use the save() function for single
Schema::create('owners', function (Blueprint model.
$table) { $valet->cars()->save($car1);
$table->increments('id'); $owner->cars()->save($car1);
$table->string('name'); // Create relation between Car and driver
}); (Valet/Owner).
Schema::create('drivers', function (Blueprint $car->valets()->attach([
$table) { $valet1->id,
$table->increments('id'); $valet2->id,
$table->integer('driver_id')->unsigned()- ]);
>index(); $car->owners()->attach([
$table->string('driver_type'); $owner1->id,
// or use $table->morphs(‘driver’); instead $owner2->id,
of "driver_id" and "driver_type" ]);
$table->integer('car_id')->unsigned()- // Or use the sync() function to prevent
>index(); duplicated relations.
$table->foreign('car_id')- $car->valets()->sync([
>references('id')->on('cars')- $valet1->id,
>onDelete('cascade'); $valet2->id,
}); ]);
$car->owners()->sync([
$owner1->id,
Retrieve Records: $owner2->id,
]);
// Get driver (Valet/Owner) Cars
$valet->cars
$owner->cars
// Get Car drivers (Valet and Owner)
$car->owners
$car->valets

You might also like