Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

laravel_has_one_relationship example

One To One Relationship in Laravel

In this tutorial, I will give you a simple example of One To One Relationship in Laravel.
so you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application. so let’s see bellow example below that will help you a lot.

Laravel One To One Eloquent Relationships

Laravel is famously known as an Eloquent ORM which is Object-Relational Mapper. A one-to-one relationship is a very basic type of database relationship.

For example, We have Two Models Category and Product, In our Category Model, we added some Categories, And in the Product Model, we added some Products with Category Id Because cat_id is the most common column in the products table.

We need to be displayed the Product with the Category name on the Product List Blade file or the Frontend Of the Application. We Define this Relationship in the Product Model.

Also, you can add the category_name related columns in the products table but it will make the table messy.

Create a one to one relationship

One to One relationship links one row in a database table to one row in another database table, This Relationship is called One to One or Has One Relationship in Laravel.

As we know a single product can have a single category name and a category that belongs to a single product,
So here we are creating a one to one relationship. This relationship is depicted in the below Example.

Categories Table

You can see in the categories table there are two fields , id and name.

One To One Relationship in Laravel

Products Table

You can see In the products table we store product_no, cat_id, name, description, and more. If you want to Generate a Unique Product Number in the Laravel Application Click Here to implements this functionality In your application.

One To One Relationship in Laravel

Create Relationship in Product Model

Let’s create One to One or Has One Relation in the Product Model, We make a Function CategoryData where we assign hasOne Relationship in Model.

  <?php

  namespace App;

  use Illuminate\Database\Eloquent\Model;

  class Product extends Model
  {
     protected $table = 'products';
     protected $fillable = ['id','product_no','cat_id','name','description'];

     public function CategoryData()
     {
     return $this->hasOne('App\Category','id','cat_id');
     }
  }

Call the ListProduct Relation in Controller

We have a ProductController, where in the ListProduct function we call the Relation From Model using ‘with’ in the Controller and pass Data in the list.blade.php file .

   <?php

   namespace App\Http\Controllers;

   use Illuminate\Http\Request;
   use App\Product;

   class ProductController extends Controller
   {
         public function ListProduct(){
         $productList = Product::with('CategoryData')->orderBy('id','desc')->get();
         return view('product.list',compact('productList'));
         }

   }

Now you can print the data, and see the attributes, You will see the CategoryData Relations with Every Single Id.

One To One Relationship in Laravel

Get the Relation Data in the Blade file.

            @foreach($productList as $key => $data)
               <tr>
                  <td>{{ $key+1 }}</td>
                  <td>{{$data->product_no}}</td>
                  <td>{{$data->name}}</td>
                  <td>{{$data->CategoryData->name}}</td>
                  <td>{{$data->description}}</td>
               </tr>
            @endforeach

Output

Now, you can see the Category name with every single product.

One To One Relationship in Laravel

In this tutorial, we learned about, One To One Relationship in Laravel, I hope this article will help you with your Laravel application Project.

Real Also : How to Check Collection is Empty or Not in Laravel ?