Skip to content

Instantly share code, notes, and snippets.

@dyaa
Forked from cullymason/Laravel_Ember.md
Created August 16, 2013 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dyaa/6251384 to your computer and use it in GitHub Desktop.
Save dyaa/6251384 to your computer and use it in GitHub Desktop.

Basic Explaination

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.

What Laravel Does

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"}]

What Ember expects

{ "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();

Relationship Overload

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','comment_ids': [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 something like:

return Post::find($id)->ember();

or maybe...

$post = Post::find($id);
return Response::ember($post);

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)

Thoughts?

Possible process of how the command would work

  • get the lowercase model name
    • pluralize if it returns more than one item (ie Model::all->ember())
  • check to see if the model has any relationships
    • if Belongs to:
      • get the singular name of the related model
      • the id of the related model
      • add the related model to the original model like so model_id:id
    • if Has Many:
      • get the plural name of the related model
      • the ids of the related model
      • add the related model to the original model like so model_ids:[id,id,id]
  • go through the different properties of the model and change from snake case to camel case (except for the relationship id property)
  • create json string that follows this pattern:{ "Model(s)": "property":"property value",..., "relationship_id(s)": id }
  • if the request asks for the sideload command (maybe: Model::all()->ember()->sideload(), the related models would be added to the json string following the same process as the original model.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment