darkcyber
4 years ago
55
5
Tips

[ASK] Help me understanding Relation Table and How to Use it ?

Posted 4 years ago by darkcyber

Hi, Sorry I Just miss understanding about relationships. In this case i want to create one to one relation, a post has many category let's assume i have POSTS table and CATEGORIES table

POSTS -> id, title, content, category
CATEGORIES -> id, name

POSTS -> 1, Network for Dummy, this is content, 1
POSTS -> 2, PHP, this is content , 2
POSTS -> 3, Computer for kids, this is content, 3

CATEGORIES -> 1,networking
CATEGORIES -> 2,programming
CATEGORIES -> 3, basic

how i can create relation in PhpMyadmin, but why we have to make relation ? what excactly laravel eloquent relationship do with this function ?

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class User extends Model
{
    /**
     * Get the phone record associated with the user.
     */
    public function phone()
    {
        return $this->hasOne('App\Phone');
    }
}

and

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class Phone extends Model
{
    /**
     * Get the user that owns the phone.
     */
    public function user()
    {
        return $this->belongsTo('App\User');
    }
}

What i think in this case is i just to input post title, content, and category id, i think i don't need to create relation. so please tell me the important thing using relation ship ?

Sorry i am new here and sory for my bad english.

Please sign in or create an account to participate in this conversation.