Object Model

The Object Model included with Laravel-Parse provides a beautiful, simple ActiveRecord implementation for working with Parse classes. Each class has a corresponding “Model” which is used to interact with that Parse class. Models allow you to query for data in your classes, as well as insert new records into parse.

Defining Object Model

The easiest way to generate a new model is to use the php artisan parse:model command:

php artisan parse:model Test

This command will generate, a new Object Model (app/Test.php) using the Laravel-Parse model stub:

<?php namespace App;

use Sirthxalot\Parse\ObjectModel;

class Test extends ObjectModel
{}

Object Model Conventions

Parse Class Name

By default Laravel-Parse expects that your Parse class will have the same name as your model has. But you can change that using the parseClassName attribute to determine the Parse class to use:

protected static $parseClassName = "Customer";

Default Master Key Usage

In Parse you may need sometimes permission to do heavy stuff, e.g. resetting the password for users or deleting rows from Parse, otherwise an exception will be thrown. This can be blocked if you determine the master key usage for your model, by adding the defaultUseMasterKey attribute to your model:

protected static $defaultUseMasterKey = true;

Create new Entry

You can use the create method to insert a new record in parse. The create method returns the saved model instance:

$parseTestClass = App\Test::create([
    'number' => 4,
    'boolean' => true,
    'string' => 'a regular string',
    'array' => [1, 2]
]);

Retrieving Object Models

Once you have created a model, you are ready to start retrieving data from your Parse class. Think of each Object Model as a powerful query builder allowing you to fluently query the Parse class associated with the model. For example:

use App\Test;

$flights = App\Test::all();

foreach ($tests as $test) {
    echo $test->string;
}

Retrieving Single Object Models / Aggregates

Of course, in addition to retrieving all of the records for a given table, you may also retrieve single records using find or first. Instead of returning a collection of models, these methods return a single model instance:

// Retrieve a model by its objectId ...
$test = App\Test::find("b9Hx6skhEX");

// Retrieve the first model matching the query constraints...
$test = App\Test::where('number', 4)->first();

Hint

You can not use find method passing in an array with key values as you may known from Laravel. Laravel-Parse doesn’t have this functionality and only will handle strings.

Modify and Delete Object Models

Please refer to A Quick Usage Example for further information about how to modify and delete Object Models.