Laravel Basic
Laravel Form
Laravel Database
Laravel Advance
A one-to-one relationship means that a row in one table is linked to exactly one row in another table.
👉 Example:
Employee: 1 ↔ 1 Profile
Flow diagram
Use this command to generate a migration for creating a tables:
php artisan make:migration create_one_to_one_tables
public function up(): void { Schema::create('employees', function (Blueprint $table) { $table->id(); $table->string('name'); $table->timestamps(); }); Schema::create('profiles', function (Blueprint $table) { $table->id(); $table->foreignId('employee_id')->constrained()->onDelete('cascade'); $table->string('bio')->nullable(); $table->timestamps(); }); } public function down(): void { Schema::dropIfExists('profiles'); Schema::dropIfExists('employees'); }
Run Migration
php artisan migrate
id | name | created_at | updated_at |
---|
id | employee_id | bio | created_at | updated_at |
---|
Here,
profiles.employee_id
is a foreign key referencing employees.id
.onDelete('cascade')
, if you delete an employee, their profile is also deleted automatically.Generate a model with Artisan:
php artisan make:model Employee php artisan make:model Profile
This generates 2 files Employee.php and Profile.php
namespace App\Models; use App\Models\Profile; use Illuminate\Database\Eloquent\Model; class Employee extends Model { protected $fillable = ['name']; // Relation: An employee has one profile public function profile() { return $this->hasOne(Profile::class); } }
namespace App\Models; use App\Models\Employee; use Illuminate\Database\Eloquent\Model; class Profile extends Model { protected $fillable = ['employee_id', 'bio']; // Relation: A profile belongs to an employee public function employee() { return $this->belongsTo(Employee::class); } }
use App\Models\Employee; // Method 1 // Create employee $employee = Employee::create(['name' => 'Watson']); // Create profile and attach to employee $employee->profile()->create([ 'bio' => 'Senior Laravel Developer', ]); // Method 2 Using Eloquent instance $employee = new Employee(); $employee->name= "John"; $employee->save(); // Create profile and attach to employee $employee->profile()->create([ 'bio' => 'PHP Developer', ]);
id | name | created_at | updated_at |
---|---|---|---|
1 | Watson | 2025-08-17 20:38:58 | 2025-08-17 20:38:58 |
2 | John | 2025-08-17 20:38:58 | 2025-08-17 20:38:58 |
id | employee_id | bio | created_at | updated_at |
---|---|---|---|---|
1 | 1 | Senior Laravel Developer | 2025-08-17 20:38:58 | 2025-08-17 20:38:58 |
2 | 2 | PHP Developer | 2025-08-17 20:38:58 | 2025-08-17 20:38:58 |
use App\Models\Employee; use App\Models\Profile; // Get employee with profile $employee = Employee::find(1); echo $employee->profile->bio; // Senior Laravel Developer // Get profile with employee $profile = Profile::find(1); echo $profile->employee->name; // John
use App\Models\Employee; // Update profile bio of employee $employee = Employee::find(1); $employee->profile->update(['bio' => 'Updated Bio']);
use App\Models\Employee; use App\Models\Profile; // Delete employee -> Profile auto deleted (cascade) Employee::find(1)->delete(); // Or delete profile only not delete employee Profile::find(2)->delete();
use App\Models\Employee; use App\Models\Profile; // Get employee with profile $employee = Employee::with('profile')->find(1); echo $employee->profile->bio; // Senior Laravel Developer // Get profile with employee $profile = Profile::with('employee')->find(1); echo $profile->employee->name; // John
Always use with()
when fetching related data in bulk.