Skip to content
  • Github
  • Facebook
  • twitter
  • About Us
  • Contact Us
  • Privacy Policy
  • Terms & Conditions
  • Site Map

XpertPhp

Expertphp Is The Best Tutorial For Beginners

  • Home
  • Javascript
    • Jquery
    • React JS
    • Angularjs
    • Angular
    • Nodejs
  • Codeigniter
  • Laravel
  • Contact Us
  • About Us
  • Live Demos
Laravel one to one eloquent relationship tutorial example

Laravel one to one eloquent relationship tutorial example

Posted on December 2, 2019December 17, 2022 By XpertPhp 121 Comments on Laravel one to one eloquent relationship tutorial example

In this tutorial, today we discuss laravel one to one Eloquent relationship. Eloquent ORM means Object-relational Mapping and laravel provides a beautiful ActiveRecord structure. so we can easy to interact with the application database. let’s start about one to one eloquent relationship.

Laravel one to one relationship is based on the relation between two tables. so we need one table of the primary key and another table of reference key. For example, we have two tables: Students and Standards. both tables are connected as a one to one relationship with each other using the primary key and reference key.

Setting Database Configuration

After complete installation of laravel. we have to database configuration. now we will open the .env file and change the database name, username, password in the .env file. See below changes in a .env file.

PHP
1
2
3
4
5
6
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name(one_to_one_relation)
DB_USERNAME=Enter_Your_Database_Username(root)
DB_PASSWORD=Enter_Your_Database_Password(root)

Create Table using migration

Now, We need to create a migration. so we will below command using create the students and standards table migration.

PHP
1
2
php artisan make:migration create_students_table --create=students
php artisan make:migration create_standars_table --create=standards

After complete migration. we need below changes in the database/migrations/create_students_table and database/migrations/create_standars_table file.

create_students_table.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStudentsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('students', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('first_name');
            $table->string('last_name');
            $table->text('address');
            $table->timestamps();
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('students');
    }
}
?>

create_standars_table.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateStandardsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('standards', function (Blueprint $table) {
            $table->bigIncrements('id');
$table->integer('student_id')->unsigned();
            $table->string('standard_name');
            $table->timestamps();
$table->foreign('student_id')->references('id')->on('students')->onDelete('cascade');
        });
    }
    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('standards');
    }
}
?>
See also  laravel 6 multiple database connections example tutorial

Run the below command. after the changes above file.

PHP
1
php artisan migrate

Create Model

Here below command help through we will create the Student and Standard model. we will also use “hasOne()” for a student model and “belongsTo()” for a standard model.

PHP
1
2
php artisan make:model Student
php artisan make:model Standard

Student.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
    protected $fillable = [
        'first_name','last_name', 'address'
    ];
/**
     * Get the standard record associated with the student.
     */
    public function standard()
    {
        return $this->hasOne('App\Standard');
    }
}
?>

Standard.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
class Standard extends Model
{
    /**
     * Get the student that owns the standard.
     */
    public function student()
    {
        return $this->belongsTo('App\Student');
    }
}
 
?>

Route and Controller

We have to need put below student resource route in routes/web.

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
<?php
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/
Route::get('/', function () {
    return view('welcome');
});
Route::get('student','studentsController@index');
 
?>

Here below command help to create the student controller.

PHP
1
php artisan make:controller StudentsController

StudentController.php

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
namespace App\Http\Controllers;
use App\Student;
use App\Standard;
use Illuminate\Http\Request;
class StudentController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
        $student1 = Student::find(1)->standard;
dd($student1);
    }
 
}
?>

Laravel, MySql Tags:eloquent relationships, laravel 6, Laravel 6 Eloquent, laravel 6 relationship, laravel 6 relationship eloquent

Post navigation

Previous Post: Laravel 6 pdf generator tutorial using dompdf
Next Post: Laravel many to many eloquent relationship tutorial example

Latest Posts

  • Laravel 12 Ajax CRUD Example
  • Laravel 12 CRUD Example Tutorial
  • How to Create Dummy Data in Laravel 11
  • Laravel 11 Yajra Datatables Example
  • Laravel 11 Ajax CRUD Example
  • Laravel 11 CRUD Example Tutorial
  • Laravel 10 Ajax CRUD Example Tutorial
  • Laravel 10 CRUD Example Tutorial
  • How to disable button in React js
  • JavaScript Interview Questions and Answers

Tools

  • Compound Interest Calculator
  • Hex to RGB Color Converter
  • Pinterest Video Downloader
  • Birthday Calculator
  • Convert JSON to PHP Array Online
  • JavaScript Minifier
  • CSS Beautifier
  • CSS Minifier
  • JSON Beautifier
  • JSON Minifier

Categories

  • Ajax
  • Angular
  • Angularjs
  • Bootstrap
  • Codeigniter
  • Css
  • Htaccess
  • Interview
  • Javascript
  • Jquery
  • Laravel
  • MongoDB
  • MySql
  • Nodejs
  • Php
  • React JS
  • Shopify Api
  • Ubuntu

Tags

angular 10 tutorial angular 11 ci tutorial codeigniter 4 image upload Codeigniter 4 Tutorial codeigniter tutorial CodeIgniter tutorial for beginners codeigniter with mysql crud operation eloquent relationships file upload File Validation form validation Image Upload jQuery Ajax Form Handling jquery tutorial laravel 6 Laravel 6 Eloquent Laravel 6 Model laravel 6 relationship laravel 6 relationship eloquent Laravel 6 Routing laravel 7 Laravel 7 Eloquent laravel 7 routing laravel 7 tutorial Laravel 8 laravel 8 example laravel 8 tutorial laravel 9 example laravel 9 tutorial Laravel Framework laravel from scratch laravel social login learn jquery nodejs pagination payment gateway php with mysql react js example react js tutorial send mail validation wysiwyg editor wysiwyg html editor

Copyright © 2018 - 2025,

All Rights Reserved Powered by XpertPhp.com