Navid
Navid

Reputation: 495

Building JSON object in desired way with Laravel

Hi There Beste Collegues,

I've having an problem building my json output structure well. What i want is the following output:

{
  comments: {
        data: {
            created_at: "date",
            other: "etc",
        from: {
            username: "navidos",
            user_id: 1
         }
       }
    }
}

But the way it is now build is:

{
    data: {
        description: "post description",
        status: "public",
        link: "http://www.bla.com",
        created_at: "2015-01-23 00:00:00",
    comments: [
    {
          text: "comment text 1",
          date: "2015-01-23 18:30:00",
          id: 1
     },
     {
          text: "comment text 2",
          date: "2015-01-23 18:35:00",
          id: 2
      },
      {
          text: "comment text 3",
          date: "2015-01-23 19:00:00",
          id: 3
       }
],
       user: {
           user_id: 1,
           username: "navid",
           profile_picture: null,
           bio: null
        }
    }
}

The output i get is almost fine but i want the comment section in this output like the first output. I've tried everything with array_merge and array push but can't figure out what i'm doing wrong here. Is there anyone who can help me out. I'm using Laravel 4 and doing my Relations with the Eloquent ORM.

So in my route i've now the following.

$post = Post::find($id);
$postComments = $post->comments;
$usersPost = $post->user;

return Response::json(
   $data = array('data'=>$post)
);

I would be very grateful if someone can help me out with this. Tnx in advanced.

Upvotes: 0

Views: 2017

Answers (2)

Navid
Navid

Reputation: 495

What i currently have in my Post.php model is:

//An post belongs to an user
    public function user(){
        return $this->belongsTo('User')->select(array('user_id', 'username','profile_picture','bio'));
    }

    //An post has many comments
    public function comments(){
        return  $this->hasMany('Comment')->select(array('comment as text','date','comment_id as id'));  
    }

and my Comment.php model looks like:

//An post belongs to an user
    public function user(){
        return $this->belongsTo('User')->select(array('user_id', 'username','profile_picture','bio'));
    }

and my User.php model looks like:

public function from(){
        return $this->hasMany('comments');   
    }

in my Route now i having the following like you suggest:

$post = Post::with('comments.from')->find($id);

but this throws an error: Missing argument 1 for Illuminate\Database\Query\Builder::from()

is there something i'm not doing correct?

Upvotes: 0

lukasgeiter
lukasgeiter

Reputation: 152900

You could simply name your relationship to User in Comment from():

public function from(){
    return $this->belongsTo('User');
}

Then eager load comments and from:

$post = Post::with('comments.from')->find($id);

This should result in something like this:

{
    description: "post description",
    status: "public",
    link: "http://www.bla.com",
    created_at: "2015-01-23 00:00:00",
    comments: [
        {
            text: "comment text 1",
            date: "2015-01-23 18:30:00",
            id: 1,
            from: {
                user_id: 1,
                username: "navid",
                profile_picture: null,
                bio: null
            }
        }
        // next comment
    ]
}

If you want to hide some of these attributes in your JSON output you can add them to the $hidden array in the model:

class Comment extends Eloquent {
    protected $hidden = ['id'];
}

Upvotes: 1

Related Questions