2014-09-08 1 views
0

'name', 'user_id', 'contact'와 같은 필드가있는 'user_profiles'와 연결된 UserProfile 모델이 있습니다. 모델에는 Eloquent ORM으로이 데이터를 검색하고 프로파일 객체를 반환하기 전에 'provinces'속성을 추가합니다.Laravel 4 : public 함수는 컨트롤러가 호출 할 때 비어 있거나 모델 데이터를 반환합니다.

class PagesController extends \BaseController { 
    public function __contstruct(UserProfile $userProfile) { 
     $this->userProfile = $userProfile; 
    } 
    public function show($id) { 
     print_r($this->userProfile->totalProfile($id)); //prints nothing 
     die; 
    } 
} 

:

class UserProfile extends Eloquent { 
    //....preceding methods ommitted 
    public function totalProfile($userid) { 
     $profile = $this->find($userid); 
     $profile->provinces = Province::all(); 
     return $profile; 
    } 
} 

나는 그것이 문제없이 프로필을 반환 내 UserProfilesController에서 위의 함수를 호출,하지만 난 내 PagesController에서 메소드를 호출 할 때 그냥과 같이, 빈 반환

왜 누군가가 이것을 설명하도록 도울 수 있다면 정말 고맙겠습니다. 고마워요!

답변

1

난 당신이이 모델에 속하는 생각하는 이유를 잘 모르겠지만, 당신이 할 경우,이 사용

모델 :

class UserProfile extends Eloquent { 

    protected $appends = ['provinces']; 

    public function getProvincesAttribute() 
    { 
     return Province::all(); 
    } 

} 

컨트롤러 :

class PagesController extends \BaseController { 

    public function __contstruct(UserProfile $userProfile) 
    { 
     $this->userProfile = $userProfile; 
    } 

    public function show($id) 
    { 
     return Response::json($this->userProfile->find($id)); 
    } 
} 
+0

제안에 감사드립니다. 모델에 접근자를 추가하는 것에 대해 알지 못했습니다. 귀하의 게시물을 읽고, 나는 [주제에 대한이 추가 게시물]을 발견 (http://stackoverflow.com/questions/17232714/add-a-custom-attribute-to-a-laravel-eloquent-model-on-load) . – bryant

+0

모델에 이것을 추가하는 것에 대한 의미를 알고 실제로 추가하고 싶지 않습니다. 모델에 편의를 위해 같은 객체의 뷰에 지방을 반환 할 수 있도록 데이터를 첨부하고 싶습니다. 지방이 아닌 지구를 포함하고 있으며, 나는 또한 지방을 보여주고 싶고 이것을 다시 생각할 것입니다. 하지만 내 문제는 다른 곳에서 생각합니다. 왜냐하면 내가 지방을 추가하기 전에 문제가있는 것 같기 때문입니다. 어쩌면 그것은 다른 컨트롤러에서 액세스하는 경우 메서드에서 $ this-> find ($ id)를 사용하는 것과 관련이 있습니까? – bryant

관련 문제