Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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 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.
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.
You can see in the categories table there are two fields , id and name.
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.
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');
}
}
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.
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
Now, you can see the Category name with every single product.
In this tutorial, we learned about, One To One Relationship in Laravel, I hope this article will help you with your Laravel application Project.