2013-05-21 6 views
1
내가 페이지에서 post_id를 (post1)에서 모든 의견 검색 할

:Laravel 4 웅변 'hasMany의'보다는 기본 검색 값을 설정하는 방법 primay 키

//localhost/posts/post1/ 

테이블

id:int[primary], comment:varchar, post_id:varchar, comment_id:varchar 
1     this is a comment post_1   comment_1 
을 코멘트를

테이블 게시물

id:int[primary], post_title:varchar, post_id:varchar 
1    this is post title post_1 

모델 comment.php

public function post() 
{ 
    return $This->belongsTo('Post'); 

} 

모델 post.php

public function show($id) 
    { 
     $comments = Post::where('post_id','=',$id)->first() 
        ->comments()->where('post_id','=',$id)->get(); 

    } 

내가 방문

public function comments() 
    { 
     return $this->hasMany('Comment'); 
    } 

컨트롤러 postsController.php

// localhost를/글/post1/어떤 관련 댓글이 표시되지 않습니다. 는 SQL은 다음과 실행 :

select * from `posts` where `post_id` = 'post1' limit 1 
select * from `comments` where `comments`.`post_id` = '1' and `post_id` = 'post1' 

어떻게 대응 주석을 검색 할 수 1'` '`post_id =을 제거 할 수 있습니까?

+0

에 의해 개별 게시물을 얻을 수 있습니다. 실행 된 SQL은 어떻게 얻었습니까? –

답변

0

게시물 테이블에서 id 열은 post_id 대신 id라는 이름을 지정해야하며 이는 laravel이 예상하는 것입니다. 웅변 모델의 기본 키를 id 이외의 값으로 설정할 수도 있지만 id를 사용하고 표준을 따르는 것이 좋습니다. 여기에있는 문서 http://four.laravel.com/docs/eloquent을 참조하십시오. id 컬럼의 이름을 id로 지정하면 Post :: find ($ id)

+0

감사합니다. 이 규칙을 재정의하기 위해 primaryKey 속성을 정의 할 수 있다는 것을 문서에서 발견했습니다. 그렇다면 어떻게 기본 키를 정의 할 수 있습니까? laravel3와 같은 방식입니까? 'public static $ key = 'studentid';'문서에서 예제를 찾을 수 없습니다. – JackpotK

+0

이제 보호 된 $ primaryKey = 'some_id';'] (https://github.com/laravel/framework/blob/master/src/Illuminate/Database/Eloquent/Model.php#L41) –