phplaravellaravel-5laravel-5.7

Returning laravel realtionship within json


I am currently doing this as title says like this:

$user = User::where('username', request('username'))->first();

$posts = [];
$comments = [];
foreach($user->posts as $post){
 foreach($post->comments as $comment){
  array_push($comments, [
    'id' => $comment->id,
    'body' => $comment->body,
    'user' => $comment->user->only(['name', 'id']),
  ]);
 }
  array_push($posts, [
    'title' => $post->title,
    'id' => $post->id,
    'body' => $post->body,
    'comments' => $comments,
  ]);
}
return response()->json([
  'user' => $user->only(['name', 'avatar', 'age']),
  'posts' => $posts,
]);

Is there a shorter way of doing this like:

$user->only(['name', 'avatar', 'age'])->withPostsOnly(['id', 'title', 'body'])->withCommentsOnly(['id', 'body']);

I know there is a way to make methods inside models that return these parts of data and then to use it same as above but shorter.

But is there any way to use something like getNameAttribute($value) for relations so I can say:

$user->only(['id', 'name', 'age', 'posts']);

And in posts value I need to have all posts and relationship data like comments and users that are connected to comments.

So basically in User model:

public function posts() {
  return $this->hasMany('App/Post')->only('id', 'title', 'body', 'comments');
}

And inside Post model:

public function comments() {
  return $this->hasMany('App/Comment')->only('id', 'body', 'user');
}

And inside Comment model:

public function comments() {
  return $this->belongsTo('App/User')->only('id', 'name');
}

Thanks


Solution

  • You are probably overcomplicating it to be honest.

    $user = User::where('username', request('username'))->first();
    $user->load(['posts.comments']);
    return response()->json($user);
    

    This is a simplified version maybe but still should indicate you can just load relationships on models.