Laravel One to One Eloquent Relationship Tutorial

  • 20-04-2022
  • 850
  • Laravel 7
  • Haresh Chauhan

Hi, Artisan

In this post you will learn how to create one to one relationships in laravel it is one of the powerful tools in laravel, laravel Provide how to create a relationship with one to another one.

One to one Eloquent create a relationship with one table to another table for example (Company and Customer, User and phone, age and hight ) it many relationships apart from that you need to use Laravel One to One Eloquent Relationship Tutorial it provides only a single relationship with other.

In this example we have created tow table 1) Company 2) Customer, and that Company relevant with the customer also given customer and company migration table and then after given controller that shows how to retrieve company and customer relationship with each other.

Here, below webappfix has described Laravel One to One Eloquent Relationship Tutorial in laravel with example and model and Controller so let's go for learn and hope that you will a lots form that with given example.

So, now I guide step by step and we can make this example.

Create company migration or what you wants create.

Laravel cache clear from view, config, route, and all cache data from application

Step 1 : Create company migration

In this step we make a company migration. So let's do that, Following below command and get migration file.

php artisan make:migration create_company_table

database/migration/YOUR_COMPANY_MIGRATION.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCompaniesTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('companies', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name');
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
        });
    }

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

Step 2 : Create customers migration

Here, In this 2nd step we need to customer migration for customer user so let's make a following below command and make migration file.

php artisan make:migration create_customer_table

database/migration/YOUR_CUSTOMER_MIGRATION.php

<?php

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateCustomersTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('customers', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->unsignedInteger('company_id');
            $table->string('name');
            $table->string('email');
            $table->string('phone');
            $table->timestamps();
        });
    }

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

Laravel pluck method example | Laravel Pluck()

Step 3 : Make A Company Model

In this step we required to company model. So let's follow the command and get Company model.

php artisan make:model Company

App/Company.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Company extends Model
{
    protected $guarded = [];
    
    public function customer()
    {
        return $this->hasOne(\App\Customer::class);
    }
}

Stpe 4 : Customer Model

Here, same as here we need to customer model, So let's follow below command.

php artisan make:model Customer

App/Customer.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Customer extends Model
{
    protected $guarded = [];

    public function company()
    {
        return $this->belongsTo(\App\Company::class);
    }
}

Step 5 : Route

In the 5th step need to one routes for run this example.

routes/web.php

Route::get('/','homeController@index');

after create route put the method and go to the HomeController.

Step 6 : Create a HomeController

Here, need to make HomeController.

So, follow below command and get HomeController

php artisan mak:controller HomeController

App\HTTP\Controllers\HomeController.php

jQuery load more data on scroll

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Customer;
use App\Company;

class HomeController extends Controller
{
    public function index()
    {
        $company = Company::find(1)->customer;
        dd($company);

       $customer = Customer::find(1)->company;
       dd($customer);
    }
}

We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :