// Create relation between Owner and Car.
$owner->car()->save($car);
// Create relation between Car and Owner.
$car->owner()->associate($owner)->save();
Retrieve Records:
// Get Owner Car
$owner->car;
// Get Car Owner
$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:
Schema::create('thieves', function (Blueprint $table) {$table->increments('id');$table->string('name');});
Schema::create('cars', function (Blueprint $table) {$table->increments('id');$table->string('name');
$thief->cars()->saveMany([$car1,$car2,]);// Or use the save() function for single model.
$thief->cars()->save($car);
// Create relation between Car and Thief.
$car->thief()->associate($thief)->save();
Retrieve Records:
// Get Thief Car
$thief->cars;
// Get Car Thief
$car->thief;
Polymorphic One to Many Relationship
Demo details:
In this demo we have 3 models (Man, Woman and Car), and 3 tables (men, women and cars).
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 it’s 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:
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:
// Create relation between buyer (Man/Woman) and Car.
$man->cars()->saveMany([$car1,$car2,]);
$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).
// Get Driver Car
$driver->cars
// Get Car Drivers
$car->drivers
Polymorphic Many to Many Relationship
Demo details:
In this demo we have 3 models (Valet, Owner and Car), and 4 tables (valets, owners,cars and 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 DriverID, DriverType and the CarID.“driver” is a name given to a group of models (Valet and Owner). And it’s 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(Valet::class, 'driver');}
public function owners()
{
return $this->morphedByMany(Owner::class, 'driver');
}
}
Database Migrations:
Schema::create('valets', function (Blueprint $table) {$table->increments('id');$table->string('name');});
Schema::create('owners', function (Blueprint $table) {$table->increments('id');$table->string('name');});
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');
});
Store Records:
// Create relation between driver (Valet/Owner) and Car.
$valet->cars()->saveMany([$car1, $car2]);$owner->cars()->saveMany([$car1, $car2]);// Or use the save() function for single model.
$valet->cars()->save($car1);$owner->cars()->save($car1);// Create relation between Car and driver (Valet/Owner).