One to one relationship in laravel

This type of relationships have two objects in which one can only belongs to the other.

First we need 2 tables which are Books and Authors. In this example every book has an author. 

php artisan make:model Author

 App/Author.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Author extends Model {

	protected $table = 'authors';

}

 In the above code we creating a model author.

Laravel 5 automatically create migration table of the correspondence model but you can do it manually.

php artisan make:migration create_authors_table

database/migrations/xxx_create_authors_table.php

<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateAuthorsTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('authors', function(Blueprint $table)
		{
			$table->increments('id');
 		    $table->string('name');
			$table->timestamps();
		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('authors');
	}

}
php artisan make:model Book

App/Book.php

<?php namespace App;

use Illuminate\Database\Eloquent\Model;

class Book extends Model {

    protected $table = 'books';

    public function author(){
        return $this->belongsTo('App\Author');
    }
}
php artisan make:migration create_books_table

Database/Migrations/xxx_create_books_table.php

<?php
class CreateBooksTable extends Migration {

	/**
	 * Run the migrations.
	 *
	 * @return void
	 */
	public function up()
	{
		Schema::create('books', function(Blueprint $table)
		{
			$table->increments('id');
            $table->string('title');
            $table->text('description');
            $table->integer('author_id')->unsigned();
	$table->timestamps();
            $table->foreign('author_id')
                ->references('id')->on('authors');

		});
	}

	/**
	 * Reverse the migrations.
	 *
	 * @return void
	 */
	public function down()
	{
		Schema::drop('books');
	}

}

Ok, We finished all Book and Author stuffs, Its time to utilization. 

App/Http/Routes.php. Redirecting information to booksInfo.blade.php

<?php
Route::get('booksInfo', function(){
    $books =  Book::all();
    return view('booksInfo', compact('books'));
});

Here is a view file booksInfo.blade.php which will display information about book and book related author.

Let's see bookInfo.blade.php file :

@foreach( $books as $book )
        <div><h1>{!! HTML::linkRoute('book.show', $book->title, array($book->id)) !!}</h1></div>
        <div>{{ $book->description }}</div>
        <div class="pull-right"><em>{{'Published by-'. $book->author->name }}</em>
        </div>
@endforeach

 No designing at all but I demonstrate necessary knowledge about Belongs To relationship.

If you see the above code then you will find the Published by portion and look at the code $book→author→name where it jumping to Book model find author function and retrieve matches author information by author_id. It gets all information as an array but here we just need author name.  

Something to say? Tell us in comment section.