3

My "user" model with a has-many relationship to an "image" model.

Returning just the user is as easy as "return Response::eloquent($user);". I'd like to either return a JSON array of {user: {id:…}, images: [{id:…},{id:…}]} or, perhaps better, {id:…, images: [{id:…},{id:…}]}

What's the best way to arrange and ship these models? Should I

1 Answer 1

11

The first thing you'll need to do is make sure your models are set up correctly:

class Users extends Eloquent {

    public function images()
    {
        return $this->hasMany('Image');
    }

}

class Image extends Eloquent {

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

}

Then in your route you should be able to do something like:

Route::any('userinfo/{$userId}', function($userId)
{
    return Response::json(User::with('images')->find($userId));

    //or

    return User::with('images')->find($userId)->toJson();
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.