This is the beginning blog post of Laravel relationship series. In this tutorial, we are going to learn how to implement One to One relationship within our Laravel application

Prerequisites

  • Laravel
  • To Continue with this tutorial, you should have installed Laravel in your pc. If you are still not installed Laravel in your machine you can configure it from here.

    Setting up the Project

    First you need to create a new laravel project by running below command in your terminal
    1
    composer create-project laravel/one-to-one-example

    Then navigate to your project directory by using below command in your terminal

    1
    cd one-to-one-example

    One to One Relationship will use “hasOne()” and “belongsTo()” for relation.

    Create Migration

    Now we have to create migration of "users" and "books" table and also add foreign key with users table.
  • Users table migration
  • 1
    2
    3
    4
    5
    6
    7
    8
    Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->string('name');
    $table->string('email')->unique();
    $table->string('password');
    $table->rememberToken();
    $table->timestamps();
    }
  • Books table migration
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    Schema::create('books', function (Blueprint $table) {
    $table->increments('id');
    $table->integer('user_id')->unsigned();
    $table->string('book');
    $table->timestamps();
    $table->foreign('user_id')->references('id')->on('users')
    ->onDelete('cascade');

    });

    Create Models

    Now we need to create User and Book table model. we will also use "hasOne()" and "belongsTo()" for relationship of both model.
  • User Model:
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    <?php

    namespace App;
    use Illuminate\Notifications\Notifiable;
    use Illuminate\Foundation\Auth\User as Authenticatable;


    class User extends Authenticatable
    {
    use Notifiable;

    protected $fillable = [
    'name', 'email', 'password',
    ];

    protected $hidden = [
    'password', 'remember_token',
    ];

    public function book()
    {
    return $this->hasOne('App\Book');
    }
    }
  • Book Model
  • 1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    <?php

    namespace App;
    use Illuminate\Database\Eloquent\Model;

    class Book extends Model
    {
    public function user()
    {
    return $this->belongsTo('App\User');
    }
    }

    Retrieve Records

    1
    2
    3
    4
    5
    $book = User::find(1)->book;
    dd($book);

    $user = Book::find(1)->user;
    dd($user);

    Create Records

    1
    2
    3
    4
    5
    6
    $user = User::find(1);

    $book = new Book;
    $book->book = 'Long walk to freedom';

    $user->book()->save($book);

    Conclusion

    In this tutorial, we learnt how to implement One to One relationship within Laravel. If you have any issue regarding this tutorial, mention your issue in comment section or reach me through my E-mail.