A cheat sheet for Laravel’s Eloquent ORM version 5.5. One to One Relationship Demo details: In this demo we have 2 models ( and ), and 2 tables ( and ). Owner Car owners cars Business Rules: The can own one .The can be owned by one . Owner Car Car Owner Relations Diagram: Relationship Details: The table should store the . Cars 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: 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: // 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 ( and ), and 2 tables ( and ). Thief Car thieves cars Business Rules: The can steal many .The can be stolen by one . Thief Cars Car Thief Relations Diagram: Relationship Details: The table should store the . Cars 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'); $table->integer('thief\_id')->unsigned()->index()->nullable(); $table->foreign('thief\_id')->references('id')->on('thieves'); }); Store Records: // Create relation between Thief and Car. $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 ( and and 3 tables ( , and ). Man, Woman Car), men women cars Business Rules: The (buyer) can buy many . The (buyer) can buy many .The can be bought by one buyer ( or ). Man Cars Woman Cars Car Man Woman Relations Diagram: Relationship Details: The table should store the and the . Car Buyer ID 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). $car1->buyer()->associate($man)->save();$car2->buyer()->associate($woman)->save(); Retrieve Records: // Get buyer (Man/Woman) Cars $men->cars$women->cars // Get Car buyer (Man and Woman) $car->buyer Many to Many Relationship Demo details: In this demo we have 2 models ( and and 3 tables ( , and a pivot table named ). Driver Car), drivers cars car_driver Business Rules: The can drive many .The can be driven by many . Driver Cars Car Drivers Relations Diagram: Relationship Details: The table “car_driver” should store the and the . Pivot Driver ID 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: 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: // Create relation between Driver and Car. $driver->cars()->attach([$car1->id,$car2->id,]); // 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,]); Retrieve Records: // Get Driver Car $driver->cars // Get Car Drivers $car->drivers Polymorphic Many to Many Relationship Demo details: In this demo we have 3 models ( and and 4 tables ( , and ). Valet, Owner Car), valets owners, cars drivers Business Rules: The (driver) can drive many . The (driver) can drive many .The can be driven by many drivers ( or/and ). Valet Cars Owner Cars Car Valet Owner Relations Diagram: Relationship Details: The table “drivers” should store the , and the . Pivot Driver ID Driver Type Car ID “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). $car->valets()->attach([$valet1->id,$valet2->id,]); $car->owners()->attach([$owner1->id,$owner2->id,]); // Or use the sync() function to prevent duplicated relations. $car->valets()->sync([$valet1->id,$valet2->id,]); $car->owners()->sync([$owner1->id,$owner2->id,]); Retrieve Records: // Get driver (Valet/Owner) Cars $valet->cars$owner->cars // Get Car drivers (Valet and Owner) $car->owners$car->valets Follow me on Twitter Mahmoud Zalt .
Share Your Thoughts