It would be nice if we had a plugin that allows Laravel to play nice with Ember. Put simply I would like to create an easy way for Laravel to output JSON strings in a way that Ember likes.
return Model::find($id);
// OR
return Model::all();
You are returned a raw json object like so:
//One Model
{"id": 1, "other attribute": "something"}
// Array of Models
[{"id": 2, "other attribute": "something"},{"id": 3, "other attribute": "something"},{"id": 1, "other attribute": "something"}]
{ "model": {"id": 1, "otherAttribute": "something"} }
or
{ "models": [{"id": 2, "otherAttribute": "something"},{"id": 3, "otherAttribute": "something"},{"id": 1, "otherAttribute": "something"}] }
The code to make this work really is not that bad:
return $a['model'] = Model::find($id)->toArray();
But it gets increasingly cumbersome and repetitive when relationships are added into the mix.
For example, if a post has many comments, Ember wants JSON to to be returned like so:
{
"post": {"id": 1,"post_title":"This is a title", "text":"post text","comments": [1,2,3]},
"comments":[
{"id":1,"post_id:"1","text":"test"},
{"id":2,"post_id:"1","text":"test"},
{"id":3,"post_id:"1","text":"test"}]
}
It would be nice if there was a way to type:
return Post::find($id)->ember();
or maybe...
$post = Post::find($id);
return Response::ember($post);
to return just the model and then to sideload relationships:
return Post::find($id)->ember()->sideload('comments');
to return
{
"post": {"id": 1,"post_title":"This is a title", "text":"post text","comments": [1,2,3]},
"comments":[
{"id":1,"post_id:"1","text":"test"},
{"id":2,"post_id:"1","text":"test"},
{"id":3,"post_id:"1","text":"test"}]
}
And it automatically does a few things:
- creates a json string in the format of "{model_name: {json representation of that model}}
- somehow distinguish if there are any relationships associated with that model
- if there are:
- Add their ids to the Model's JSON string
- create a json array of all of those relationships
- and recursively add ids and json arrays if those relationships have them (For example,if a category has posts and those posts have comments)
Good call. I wish there was an ember method with Eloquent, but I doubt it will be implemented by the core team. I guess it can be done through a base model...
Let me give it a try. I'll keep you updated.