Including additional custom attributes in an laravel eloquent response -
model
class profile extends eloquent { public function user(){ return $this->belongsto('user'); } public function url(){ return url::route('profile', array('city_slug' => $this->city_slug, 'slug'=> $this->slug)); } }
controller
class profilecontroller extends basecontroller { public function show($user_id) { $profile = profile::with('user')->where('user_id', $user_id); return response::json($profile, 200); } }
i response include generated url profile method url(){}
{ "id" : 1, "url" : /profile/{city_slug}/{slug}, .... }
thanks in advance.
i go @ other way.
in users model...
public function profile() { return $this->belongsto('profiles','profile_id'); }
then can use $profile = user::find(user_id)->profile
.
$data = array( 'id' => $profile->id, 'url' => "/profile/".$profile->city_slug."/".profile->slug );
and return repsonse::json($data,200);
Comments
Post a Comment