Laravel's famous Eloquent ORM is an Object-Relational Mapper and this tutorial will demonstrate how One To One relationships work in Laravel with an example. Join me while I show you what they are
Laravel One to One Relationship
One-to-one relationships play a fundamental role in the Laravel framework. One instance of the UserModel has only one BankAccount, so it has a single account number. Thus, we can connect both models - User and Bank - as a one-to-one relationship with each other. Let's put the account method into the User model, and that account only belongs to one User. So in the User model, we can call the account method and that one call hasOneMethod.
Step 1: Create account details Schema.
php artisan make:migration create_account_table
Once you enter above command in terminal you will find the one schema in which we need to define columns as follow.
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateAccountTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('account', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->integer('account_number');
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('account');
}
}
Now, run following command in the terminal.
php artisan migrate
Above command will create table in MySQL database.
Step 2: Insert the values in the table.
Ideally we need to insert values into two tables as follows, but I assume u have already records in users table so we will focus on account table only.
users
account
Run following commands in your terminal.
php artisan make:seeder AccountsTableSeeder
<?php
use Illuminate\Database\Seeder;
class AccountTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
DB::table('account')->insert([
'user_id' => 1,
'account_number' => 1111111111
]);
DB::table('account')->insert([
'user_id' => 2,
'account_number' => 2222222222
]);
DB::table('account')->insert([
'user_id' => 3,
'account_number' => 3333333333
]);
DB::table('account')->insert([
'user_id' => 4,
'account_number' => 4444444444
]);
DB::table('account')->insert([
'user_id' => 5,
'account_number' => 5555555555
]);
}
}
Now, add the following code in the DatabaseSeeder.php file.
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$this->call(AccountTableSeeder::class);
}
Run following commands in your terminal.
php artisan db:seed
Now table is filled with values.
Step 3: Define a one-to-one relationship.
Need to create one model called Account.php by using below command.
php artisan make:model Account
Need to define relationship with account table for that write below code to user.php file.
public function account()
{
return $this->hasOne('App\Account');
}
If we want to interact with the database, Laravel provides one command-line interface called tinker. To boot up, the tinker hit the following command
php artisan tinker
Now, enter the following in the tinker.
$account = User::find(1)->account;
The app will display account details related to user 1. Each user has only one single account. Eloquent determines a foreign key of a relationship based on a model name
The Account model is automatically assumed to have a user ID foreign key. You can specify the ID column if you want to override this convention.
return $this->hasOne('App\Account', 'foreign_key');
So, we can access the Account model from a User. Now we need to define a relationship on the Account model that will allow us to get its owner. We can use the belongsTo method to determine an inverse of this relationship on the User model.
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Account extends Model
{
public function user()
{
return $this->belongsTo('App\User');
}
}
Eloquent will try to match a user id from an Account model to an id on the User model in the example above. The ORM will determine a default foreign key name by examining the name of a relationship method and suffixing the method name with "id".
However, if a foreign key on the Account model is not a user id, you may pass the custom key name as the second argument to a BelongsTo method.
I hope you will like the content and it will help you to learn Laravel One to One Eloquent Relationship Tutorial
If you like this content, do share.