I have in my User
model a posts()
method that return me all the Post
. It's enable me to get really easily all posts in a json:
return response()->json(User::find($id)->posts()->get(), 200);
The method is written like that:
public function posts()
{
$posts= $this->hasMany('App\Post');
return $posts;
}
Problem is that I want to makeVisible
some attributes of all my posts. So I use the each()
method just like that:
public function posts()
{
$posts= $this->hasMany('App\Post');
$posts->each(
function ($post, $key) {
$post->makeVisible(['hiddenAttribute', ...]);
}
);
return $posts;
}
But I don't see any changes in my response. Did I miss something?