Laravel 6.2 Eloquent Relationship Tutorial

Today, We want to share with you Laravel 6.2 Eloquent Relationship Tutorial.In this post we will show you Laravel One To One Eloquent Relationships Tutorial Example, hear for laravel 6.2 polymorphic one to one we will give you demo and example for implement.In this post, we will learn about laravel 6.2 one to one relationship, with an example.

Laravel 6.2 Eloquent Relationship Tutorial

There are the Following The simple About Laravel Eloquent One to One Relationship Tutorial with Example Full Information With Example and source code.

As I will cover this Post with live Working example to develop How to Implement Laravel Eloquent One to One Relationship, so the laravel 6.2 one to many relationship with pivot table is used for this example is following below.

Read Also:  Laravel 6 csrf tokens in ajax calls

Phase 1 : Install Laravel 6.2 and Basic Configurations

Install Laravel 6.2 and Basic some IMP. Configurations

composer create-project --prefer-dist laravel/laravel adminpanel-api-project

Types of Eloquent Relationships in Laravel 6.2

  • One To One
  • One To Many
  • Many To Many
  • HasMany Through
  • Many To Many Polymorphic Relation

Phase 2 : Create Laravel 6.2 Migration and Model

php artisan make:model Pincode -m

create_pincodes_table.php
public function up()
{
    Schema::create('pincodes', function (Blueprint $table) {
        $table->bigIncrements('id');
        $table->unsignedBigInteger('member_id');
        $table->string('pincode');
        $table->timestamps();

        $table->foreign('member_id')->references('id')->on('users')
            ->onDelete('cascade');
    });
}

Phase 3 : Setup One To One Relationship using Laravel 6.2

app/Member.php

<?php

namespace App;

use Illuminate\Foundation\Auth\Member as Authenticatable;
use Illuminate\Notifications\Notifiable;

class Member extends Authenticatable
{
    // ...

    /**
     * Get the pincode number associated with the member.
     */
    public function pincode()
    {
        return $this->hasOne(Pincode::class);
        // note: we can also inlcude Pincode model like: 'App\Pincode'
    }
}

Phase 4 : Laravel Inverse Of The Relationship

app/Member.php

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Pincode extends Model
{
    /**
     * Get the member that owns the pincode.
     */
    public function member()
    {
        return $this->belongsTo(Member::class);
    }
}

Phase 5 : Laravel Insert Records

MemberController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Member;
use App\Pincode;
use Hash;

class MemberController extends Controller
{
    public function addMemberPincode()
    {
        $member = new Member;
        $member->name = "TamilRokers";
        $member->email = "[email protected]";
        $member->password = Hash::make("3602899898");
        $member->save();

        $pincode = new Pincode;
        $pincode->pincode = '3600001';
        $member->pincode()->save($pincode);
    }
}

Phase 6 : Laravel Retrieve Records

public function index()
{
    // get member and pincode data from Member model
    $member = Member::find(1);
    var_dump($member->name);
    var_dump($member->pincode->pincode);

    // get member data from Pincode model
    $member = Pincode::find(1)->member;
    dd($member);

    // get pincode number from Member model
    $pincode = Member::find(1)->pincode;
    dd($pincode);
}

Phase 7: Laravel Update Records

update the member’s table data and pincode’s table data at once:

public function update()
{
    $member = Member::find(1);

    $member->name = 'Bollywood Khans';
    $member->pincode->pincode = '3600054';
    $member->push();
}

Phase 8 : Laravel Delete Records

delete both table’s data at once

public function delete()
{
    $member = Member::find(1);
    $member->delete();
}

Web Programming Tutorials Example with Demo

Read :

Read Also:  country state city drop down list using JavaScript

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about laravel 6 update related model.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel Advanced Eloquent Tips and Tricks
  2. Read Also:  how to insert date in sql?
  3. Read Also:  Bootstrap Toggle switch with ajax update to mysql in Laravel
  4. Read Also:  Simple Vue js CRUD Example Tutorial From Scratch
  5. Read Also:  PHP mysqli_num_fields - Function MySQLi Num Fields
  6. Read Also:  Laravel Autocomplete text field suggestions search using Database
  7. Read Also:  AngularJS populate Dynamic Dependent dropdown in PHP
  8. Read Also:  Laravel Eloquent Useful Tips and Tricks
  9. Read Also:  Laravel Many to many relationships with itself
  10. Read Also:  Simple Directives using Angular Example