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.

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

hasOne(Pincode::class);
        // note: we can also inlcude Pincode model like: 'App\Pincode'
    }
}

Phase 4 : Laravel Inverse Of The Relationship

app/Member.php

belongsTo(Member::class);
    }
}

Phase 5 : Laravel Insert Records

MemberController.php

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 :

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.

Leave a Comment