Support the ongoing development of Laravel.io →
posted 10 years ago
Database

I want to create one to one relationship in mysql database but I can't find any solution.

User tables:

Author tables:

Schema::create('authors', function(Blueprint $table)
		{
			$table->engine = 'InnoDB';
			$table->increments('id');
			$table->string('name');
                        $table->integer('user_id')->unsigned()->nullable();
			$table->foreign('user_id')->references('id')->on('users');
			$table->timestamps();
		});

User table

Schema::create('users', function(Blueprint $table)
		{
			$table->engine = 'InnoDB';
			$table->increments('id');
                        $table->string('email');            
                        $table->string('password');
                        $table->string('remember_token',100)->nullable();
                        $table->timestamps();
		});

And define in model Author:

public function user()
    {
        return $this->belongs_to('App\User');
    }

But when I run php artisan migrate it create one to many relationship in database from users table to author table.

So how to create one to one relationship between them??

Thanks a lot.

Last updated 3 years ago.
0

Sign in to participate in this thread!

Eventy

Your banner here too?

taubjdjn taubjdjn Joined 3 Jun 2015

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.