In this tutorial you will learn about the Laravel One to One Relationship Example and its application with practical example.
In this Laravel one to one relationship example tutorial, I’ll show you to implement one to one relationships In laravel eloquent model. You will also learn how to create and use one to many relationship in laravel.
Laravel One to One Relationship Example
The one to one relationship is a very common relationship in laravel model. In this example we will learn to implement and use one to one relationship in laravel. We will also show you how to insert, retrieve, update, and delete data with the eloquent model from the database table in laravel.
Laravel Eloquent One to One Relationship Example
Let suppose we have two tables posts and contents in our example tutorial. In laravel we can create both tables using migration as following:
Post table migration:
1 2 3 4 5 6 7 8 9 10 11 |
Schema::create('posts', function (Blueprint $table) { $table->increments('id'); $table->string('title'); $table->text('short_desc'); $table->timestamps(); }); |
Contents table migration:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
Schema::create('contents', function (Blueprint $table) { $table->increments('id'); $table->integer('post_id')->unsigned(); $table->text('description'); $table->timestamps(); $table->foreign('post_id')->references('id')->on('posts') ->onDelete('cascade'); }); |
In this example, the Post model is closely associated with a Content model in one to one relationship. In one to one relationship we will create a post_content() method within the Post model and there is hasOne() method to relate it with Content model as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { public function post_content() { return $this->hasOne('App\Content'); } } |
The Eloquent model adds a foreign key based on the model name and should have a matching id value in it. In this relationship the post_id is the foreign_key for Content model,
Inverse of One to One Relationship Example
For now we can have the access to the content from the post model. Now we create an inverse relationship in the content model. For this we will use the belongsTo method for getting the post data from the content model as following.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Content extends Model { public function post() { return $this->belongsTo('App\Post'); } } |
We can now access the post content using the relation method as following:
1 |
$content = Post::find(10)->post_content; |
Laravel Insert record in one to one relationship
You can insert record in post and content table using one to one relationship as following:
1 2 3 4 5 6 7 8 9 10 11 |
public function addPost() { $post = new Post; $post->title= "Hello world"; $post->short_desc= "Hello world"; $post->save(); $content = new Content; $content->description= 'helllo world post description'; $content->post_content()->save($content); } |
Now, if you want to retrieve data from both post and content table, you can use one to one relationship as following:
1 2 3 4 5 6 7 8 9 10 11 12 |
public function index() { // get post and content from Post Model $post = Post::find(1); var_dump($post->title); var_dump($post->post_content->description); // get post data from PostContent model $post= Content::find(1)->post; dd($post); } |
Laravel Delete record in One to One relationship
Now, to delete record from both tables posts and contents in one to one relationship, you can do it as following:
1 2 3 4 5 |
public function delete() { $post = Post::find(1); $post->delete(); } |