Reputation: 2051
I have a UserProfile model with associated table 'user_profiles' that has some fields like 'name', 'user_id', 'contact'. In the model there is a function which retrieves this data with the Eloquent ORM and adds an additional property 'provinces' to it before returning the profile object.
class UserProfile extends Eloquent {
//....preceding methods ommitted
public function totalProfile($userid) {
$profile = $this->find($userid);
$profile->provinces = Province::all();
return $profile;
}
}
When I call the above function from my UserProfilesController it returns the profile with no problem, but when I try to call the method from my PagesController it just returns empty, like so:
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;
}
}
I would really appreciate it if someone could help explain why this is? Thank you very much!
Upvotes: 0
Views: 161
Reputation: 219936
I'm not sure why you think this belongs on your model, but if you do, use this:
Model:
class UserProfile extends Eloquent {
protected $appends = ['provinces'];
public function getProvincesAttribute()
{
return Province::all();
}
}
Controller:
class PagesController extends \BaseController {
public function __contstruct(UserProfile $userProfile)
{
$this->userProfile = $userProfile;
}
public function show($id)
{
return Response::json($this->userProfile->find($id));
}
}
Upvotes: 1