Lavel에서 웅변으로 사용하는 동일한 두 테이블에 대해 많은 많은 사람들을 창출합니까?

StackOverflow https://stackoverflow.com//questions/21022111

문제

2 개의 테이블 '사용자'와 '콘텐츠'가 있습니다. 사용자가 좋아하는 콘텐츠를 즐길 수 있으므로 Pivot Table user_content를 사용하여 사용자 간의 많은 관계와 콘텐츠가 꺼린 콘텐츠를 설정합니다. 이것은 괜찮습니다.

사용자는 콘텐츠를 구입할 수도 있습니다.이것은 위 (User_Content)와 동일한 관계가 될 것이지만 완전히 다른 데이터가됩니다.

테이블 간의 관계가 동일하지만 내용이 다르지만 이런 관계는 어떻게 enokent를 사용하여 Laravel에서 성공적으로 수행 할 수 있습니까? 아니면 완전히 다른 방식으로 이에 접근해야합니까?

도움이 되었습니까?

해결책

user_id 및 content_id 필드를 포함하는 user_purchase 및 user_favorite 테이블을 설정합니다

모델

모델 설정

class User extends Eloquent{
    public function purchases()
    {
        return $this->belongsToMany('Content','user_purchase');
    }

    public function favorites()
    {
        return $this->belongsToMany('Content','user_favorite');
    }
}

class Content extends Eloquent{

    public function buyers()
    {
        return $this->belongsToMany('User','user_purchase');
    }

    public function favorites()
    {
        return $this->belongsToMany('User','user_favorite');
    }
}
.

두 번째 열은 많은 많은 관계를 확인하는 테이블을 나타냅니다

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top