2016-07-15 7 views
0

나는 레거시 db를 제거하려고하고 완전히 혼란스러워하고있다. (Laravel 5.2 모델 구조

카드, 범주와 cards2categories

카드는 많은 종류 그래서 카드 모델에 배치 한

에 속할 수 있기 때문입니다 외국를 :

내가 걱정하는 세 개의 테이블이 키가 이미지 임)

컨트롤러에서 시도했습니다.

$cards = DB::table('cards')->categories->get(); 

그러나 작동하지 않았습니다.

내 다음 문제는 card2cat이 이름이 아닌 숫자를 반환한다는 것입니다.

답변

1

이 다음

$cards = Cards::with('categories')->get(); 

foreach($cards as $card){ 

    //$card->categories will contain the array of all categories rows 
    //associated with this card and you can loop through it. 

    foreach($card->categories as $category){ 
     //do something maybe? 
    } 
} 
을 시도 검색어에 대한 클래스 선언

use App\Cards; //or whatever is your model name 

이 사용 추가