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:

<?php namespace App;

use Sirthxalot\Parse\Auth\UserModel as Authenticatable;

class User extends Authenticatable
{
    protected static $defaultUseMasterKey = true;

    /**
     * Get the test record associated with the user.
     */
    public function test()
    {
        return $this->belongsTo('App\Test');
    }
}

Next you will need to add a pointer field to your parse class:

../_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:

public function test()
{
  return $this->belongsTo('App\Test', 'related_test');
}

Now you will be able to get the relationship using the test method:

$query = new Query('_User', 'App\User', true);

$query = $query->findOrFail('TtlCRVV9ZK');

return $query->test();

Should return something like the following:

../_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:

<?php namespace App;

use Sirthxalot\Parse\ObjectModel;

class Post extends ObjectModel
{
    /**
     * Get the comments for the blog post.
     */
    public function comments()
    {
        return $this->belongsToMany('App\Comment', 'comments');
    }
}

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:

<?php namespace App;

use Sirthxalot\Parse\ObjectModel;

class Comment extends ObjectModel
{
    /**
     * Get the post related to the comment.
     */
    public function post()
    {
        return $this->belongsTo('App\Post');
    }
}

In Parse you will need to create a Post class and a Comment class. The Post class will need a relation field:

../_images/parse-post-comment-relation.png

And the Comment class will need a pointer field:

../_images/parse-comment-post-pointer.png

Now you will be able to get the relationship using the post or comments method:

$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());