2014-09-26 4 views
2

나는 다음과 같은 테이블, 데이터베이스 관계를 결정하는 것을 시도하고있다 : 각 게시물의 GANNA는 하나 개의 유형이 type_idposts_type (ID)가 동일 Laravel 쿼리 관계

posts 
====== 
id 
type_id 
title 
content 


posts_type 
========== 
id 
type_name 

, 방법

을 Laravel에서 이것을 일대일 관계라고 정의합니까?

모델 PostType :

class PostType extends Eloquent { 

    protected $table = 'posts_type'; 

    public function posts() 
    { 
     return $this->hasMany('Post', 'type_id', 'id'); 
    } 

} 

// Get PostType (whereId=1) with all related posts 
$postType = PostType::with('posts')->find(1); 

모델 Post :

감사

답변

2

당신은 예를 들어 One-To-Many 관계를 사용할 수 있습니다

class Post extends Eloquent { 

    protected $table = 'posts'; // Optional 

    public function postType() 
    { 
     return $this->belongs('PostType', 'type_id', 'id'); 
    } 

} 

// Get Post (whereId=1) with it's related PostType 
$post = Post::with('postType')->find(1); 
+0

사람들은 두 개의 별도 modles를 잘입니까? – user3150060

+0

예, 둘 다 다른 모델입니다. –

+0

도움을 주셔서 대단히 감사합니다. – user3150060