Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
A Polymorphic One-to-One relationship allows a model to be associated with exactly one related model, but that related model can belong to multiple different parent models.
👉 Example:
logos
table via polymorphic one-to-one.So the relation is:
Company → Logo School → Logo
Flow diagram
Use this command to generate a migration for creating a tables:
php artisan make:migration create_polymorphic_one_to_one_tables
public function up(): void { // Companies table Schema::create('companies', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); // Schools table Schema::create('schools', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); // Logos table (polymorphic) Schema::create('logos', function (Blueprint $table) { $table->id(); $table->string('image'); $table->morphs('logoable'); // logoable_id & logoable_type $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('logos'); Schema::dropIfExists('schools'); Schema::dropIfExists('companies'); }
Run Migration
php artisan migrate
id | name | created_at | updated_at |
---|
id | name | created_at | updated_at |
---|
id | name | logoable_id | logoable_type | created_at | updated_at |
---|
Generate a model with Artisan:
php artisan make:model Company php artisan make:model School php artisan make:model Logo
This generates 3 files Company.php , School.php and Logo.php
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Company extends Model { protected $fillable = ['name']; public function logo() { return $this->morphOne(Logo::class, 'logoable'); } }
namespace App\Models; use Illuminate\Database\Eloquent\Model; class School extends Model { protected $fillable = ['name']; public function logo() { return $this->morphOne(Logo::class, 'logoable'); } }
namespace App\Models; use Illuminate\Database\Eloquent\Model; class Logo extends Model { protected $fillable = ['image']; public function logoable() { return $this->morphTo(); } }
use App\Models\Company; use App\Models\School; // Create a Company with Logo $company = Company::create(['name' => 'Tech Corp']); $company->logo()->create(['image' => 'tech_logo.png']); // Create a School with Logo $school = School::create(['name' => 'Green Valley School']); $school->logo()->create(['image' => 'school_logo.png']);
id | name | created_at | updated_at |
---|---|---|---|
1 | Tech Corp | 2025-08-25 14:55:55 | 2025-08-25 14:55:55 |
id | name | created_at | updated_at |
---|---|---|---|
1 | Green Valley School | 2025-08-25 14:55:55 | 2025-08-25 14:55:55 |
id | name | logoable_id | logoable_type | created_at | updated_at |
---|---|---|---|---|---|
1 | tech_logo.png | 1 | App\Models\Company | 2025-08-25 14:55:55 | 2025-08-25 14:55:55 |
2 | school_logo.png | 1 | App\Models\School | 2025-08-25 14:55:55 | 2025-08-25 14:55:55 |
// Get Company Logo $company = Company::find(1); echo $company->logo->image; // tech_logo.png // Get School Logo $school = School::find(1); echo $school->logo->image; // school_logo.png // Get Logo Owner (Company or School) $logo = Logo::find(1); echo $logo->logoable->name; // Tech Corp
// Update Company Logo $company = Company::find(1); $company->logo->update(['image' => 'tech_logo_updated.png']); // Update School Logo $school = School::find(1); $school->logo->update(['image' => 'school_logo_updated.png']);
// Delete a Logo $company = Company::find(1); $company->logo->delete(); // Delete a School (Logo will remain unless handled manually) $school = School::find(1); $school->delete();
// Get Companies with Logos $companies = Company::with('logo')->get(); // Get Schools with Logos $schools = School::with('logo')->get(); // Get Logos with parent model (Company/School) $logos = Logo::with('logoable')->get();