Relationships ============= Parse classes are often related to one another. For example, a blog post may have many comments, or an order could be related to the user who placed it. Object model makes managing and working with these relationships easy, and supports several different types of relationships: * One to One * One to Many One To One ---------- A one-to-one relationship is a very basic relation. For example, a `User` model might be associated with one `Test`. To define this relationship, we place a `test` method on the `User` model. The `test` method should call the `belongsTo` method and return its result: .. literalinclude:: ../references/UserFinal.php :language: php Next you will need to add a pointer field to your parse class: .. image:: ../images/parse-pointer-test.png By default Laravel-Parse expects that the field name is the same like the model name in lower-case (here: `App\\Test` => `test`). But you can change the key if you set a second argument to the `belongsTo` method: .. code-block:: php public function test() { return $this->belongsTo('App\Test', 'related_test'); } Now you will be able to get the relationship using the `test` method: .. code-block:: php $query = new Query('_User', 'App\User', true); $query = $query->findOrFail('TtlCRVV9ZK'); return $query->test(); Should return something like the following: .. image:: ../images/parse-pointer-test-result.png One To Many ----------- A "one-to-many" relationship is used to define relationships where a single model owns any amount of other models. For example, a blog post may have an infinite number of comments. Like all other Laravel-Parse relationships, one-to-many relationships are defined by placing a function on your object model: .. literalinclude:: ../references/Post.php :language: php Like before, the **first argument** will determine another object model that is related to your object model. The **second argument** can be used in order to determine the foreign key to use, in this case `comments`. Now you can determine that a comment belongs to a post, like we have seen before: .. literalinclude:: ../references/Comment.php :language: php In Parse you will need to create a `Post` class and a `Comment` class. The `Post` class will need a relation field: .. image:: ../images/parse-post-comment-relation.png And the `Comment` class will need a pointer field: .. image:: ../images/parse-comment-post-pointer.png Now you will be able to get the relationship using the `post` or `comments` method: .. code-block:: php $query = new Query('Comment', 'App\Comment', true); $query = $query->findOrFail('AM0iE0ISMj'); dd($query->post()); // or $query = new Query('Post', 'App\Post', true); $query = $query->findOrFail('zJFlef2kcU'); dd($query->comments());