Laravel Model One-to-One Relationship
While working with laravel application we need to deal with database. We usually don't interact directly with database. Instead we mainly use laravel model. In this tutorial we will use one to one relationship and try to show how it works mainly. Let's start.
Before starting let's create an empty laravel 11 project
composer create-project --perfer-dist laravel/laravel laravel-model-relation
Switch to laravel-model-relation directory
cd laravel-model-relation
In this blog we will use user and bank account example. Let's create model and migration for User and Bank Account. Below is an ER diagram for easy understanding the scenario
User Model
namespace App\Models; // use Illuminate\Contracts\Auth\MustVerifyEmail; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Foundation\Auth\User as Authenticatable; use Illuminate\Notifications\Notifiable; class User extends Authenticatable { use HasFactory, Notifiable; protected $fillable = [ 'name', 'email', 'password', ]; protected $hidden = [ 'password', 'remember_token', ]; protected function casts(): array { return [ 'email_verified_at' => 'datetime', 'password' => 'hashed', ]; } public function bank_account() { return $this->hasOne(BankAccount::class,'bank_account_id','id'); } }
Users table Migration
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { public function up(): void { Schema::create('users', function (Blueprint $table) { $table->id(); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); $table->foreign('bank_acount_id')->references('id')->on('bank_accounts') ->onDelete('cascade')->onUpdate('cascade'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('password_reset_tokens'); } };
BankAccount Model
namespace App\Models; use Illuminate\Database\Eloquent\Model; class BankAccount extends Model { public function user() { return $this->belongsTo(User::class); } }
Bank Account Migration
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; return new class extends Migration { /** * Run the migrations. */ public function up(): void { Schema::create('bank_accounts', function (Blueprint $table) { $table->id(); $table->string('bank_name'); $table->string('account_name'); $table->string('account_number'); $table->string('account_type'); $table->unsignedBigInteger('user_id'); $table->timestamps(); $table->foreign('user_id')->references('id')->on('users') ->onDelete('cascade')->onUpdate('cascade'); }); } /** * Reverse the migrations. */ public function down(): void { Schema::dropIfExists('bank_accounts'); } };
Now run migrate
php artisan migrate
Insert user data along with bank account information using Model
// Creating a user $user = User::create([ 'name' => 'Programmingmindset', 'email' => '[email protected]', 'password' => Hash::make(12345678), ]); $bankAccount = new BankAccount([ 'bank_name' => 'ABC Bank', 'account_name' => 'Programmingmindset.com', 'account_number' => '12444444444560', 'account_type' => 'checking', ]); $user->profile()->save($bankAccount);
Access User Bank Account by Object Chaining Relationship
$user = User::find(10); $userBankAccount = $user->bank_account;
One to one relationship is mainly used for single relation. You can practice and integrate in your application according to need. Hope , this tutorial will help you a lot to understand laravel one to one relationship.
You can find demo code here in this link https://github.com/Programming-Mindset/laravel-model-relation