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.

List of all Google Adsense, VueJS, AngularJS, PHP, Laravel Examples.

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

<?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 :

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.   how to create plugin in wordpress with example
  2.   Requests has() and hasFile() in Laravel 6
  3.   Laravel 6 Eloquent Has With and WhereHas
  4.   Advanced Laravel 5.7 Eloquent Queries Builder
  5.   Laravel Advanced Eloquent Tips and Tricks
  6.   Event Watch route change with VueJS
  7.   Add Edit Delete Table Row Example using JQuery
  8.   How to display break in Laravel blade properly?
  9.   Laravel Many to many relationships with itself
  10.   Laravel Eloquent Useful Tips and Tricks

Leave a Reply

Your email address will not be published. Required fields are marked *