This library pretends to make Parse usable in a Eloquent-like manner. For Laravel 5.2+.
- Initialize Parse automatically.
- Use facade classes that wraps Parse's classes, exposing an Eloquent-like interface.
- Enabled to work with Parse's relations.
- User authentication with username/password combinations and/or with Facebook.
- Command to create ObjectModels (
artisan parse:model Foo
).
Add the service provider in your config/app.php
file:
'providers' => [
// etc...
Parziphal\Parse\ParseServiceProvider::class,
],
Publish the configuration file by running:
php artisan vendor:publish --tag=parse
Set your Parse server configuration in config/parse.php
, or preferably in your .env
file by setting the following envs:
PARSE_APP_ID=App_ID
PARSE_REST_KEY=REST_API_key
PARSE_MASTER_KEY=Master_key
PARSE_SERVER_URL=http://127.0.0.1:1337
PARSE_MOUNT_PATH=/parse
Note: On Laravel 5.4 the web middleware group has an entry for
\Illuminate\Session\Middleware\AuthenticateSession
(which is disabled by default). Activating this middleware will cause the "remember me" feature to fail.
Make sure your User class extends Parziphal\Parse\UserModel
. You could extend instead from Parziphal\Parse\Auth\UserModel
, which is a authentication-ready User class:
namespace App;
use Parziphal\Parse\Auth\UserModel;
class User extends UserModel
{
}
Now we have to configure both the web guard and the users provider, so open config/auth.php
, and make the following changes:
'guards' => [
'web' => [
'driver' => 'session-parse',
'provider' => 'users',
],
// ...
],
'providers' => [
'users' => [
'driver' => 'parse',
'model' => App\User::class,
],
// ...
],
There are 3 provider drivers available:
parse
which requires users to have a username and a passwordparse-facebook
which requires users to identify using their Facebook accountparse-any
which lets users authenticate with either username/password or Facebook
You can use the Parziphal\Parse\Auth\AuthenticatesWithFacebook
trait in your auth controller along with (not instead of) Laravel's Illuminate\Foundation\Auth\AuthenticatesUsers
trait. The AuthenticatesWithFacebook
trait has methods to handle Facebook authentication/registration. Just bind the method (or methods) you need to a route and you're ready to go.
Below is the interface of the authentication/registration trait. Note that it can respond in two ways: with a redirection (the *Redirect methods), or with JSON (the *Api methods), which will respond with the $apiResponse
array.
trait AuthenticatesWithFacebook
{
protected $apiResponse = ['ok' => true];
public function logInOrRegisterWithFacebookApi(Request $request);
public function logInOrRegisterWithFacebookRedirect(Request $request);
public function registerWithFacebookApi(Request $request);
public function registerWithFacebookRedirect(Request $request);
public function registerAny(Request $request);
public function logoutApi(Request $request);
// For logout with redirection simply use logout().
}
For Facebook login, the trait expects to find the user's Facebook ID as the id
parameter, and their access token as the access_token
parameter.
There are things to take into consideration regarding this:
-
The validator returned by the
validator
method of the registration controller has aunique
constraint on theemail
, which will trigger database searches, leading to an error; make sure to remove thatunique
constraint. -
You'll also have to change the
create
method according to your needs. It could look like this (specially if you're using the auth scaffold):
protected function create(array $data)
{
$user = new User();
$user->name = $data['name'];
$user->username = $data['email'];
$user->password = $data['password'];
$user->signUp();
return $user;
}
Remember that on Parse, the username
field is the login name of the user, so you'll have to store their email under the username
key if you require users to login using their email.
The Parziphal\Parse\ObjectModel
class is a wrapper for Parse\ParseObject
. It behaves as an Eloquent model, so you could do stuff like:
// Instantiate with data
$post = new Post(['title' => 'Some Title']);
// Create
$post = Post::create(['title' => 'Some Title', 'acl' => $acl]);
// Get objectId
echo $post->id; // EWFppWR4qf
echo $post->id(); // EWFppWR4qf
// Update
$post->title = "New Title";
$post->save();
// or
$post->update(['foo' => true]);
// Find or fail
$post = Post::findOrFail($id);
// Get all records
$posts = Post::all();
// Delete is like Eloquent's delete: it will delete the object
$post->delete();
// To remove a key (ParseObject's `delete` method), use `removeKey`
$post->removeKey($someKey);
// Create a pointer object
$pointer = Post::pointer($postId);
Parziphal\Parse\Query
is a wrapper for Parse\ParseQuery
, which also behaves like a Eloquent Builder:
// Note that `get` is like Eloquent Builder's `get`, which executes the query,
// and not like ParseQuery's `get` which finds an object by id.
$posts = Post::where('createdAt', '<=', $date)->descending('score')->get();
$posts = Post::where([
'creator' => $user,
'title' => $title
])
->containedIn('foo', $foos)
->get();
$post = Post::firstOrCreate($data);
// Load relations (ParseQuery::include())
$posts = Post::with('creator', 'comments.user')->get();
Objects and queries can be configured to use Master Key with the $useMasterKey
property. This can be done at class level, at instantiation, or by using the setter method:
// In objects, pass a second parameter when instantiating:
$post = new Post($data, true);
// or use the setter method:
$post->useMasterKey(true)->save();
// Passing an anonymous function will set useMasterKey to true,
// then execute the function, then useMasterKey will be set to false.
$post->useMasterKey(function($post) {
$post->increment('views')->save();
});
// When creating queries, pass as parameter:
$query = Post::query(true);
// or use the setter method:
$query->userMasterKey(true);
// Other object methods that accept a $useMasterKey value are:
$post = Post::create($data, true);
$posts = Post::all(true);
// To configure a model to always use Master Key, define
// a protected static property `$defaultUseMasterKey`:
class Post extends ObjectModel
{
protected static $defaultUseMasterKey = true;
}
// Or use this to make all models use master key by default
ObjectModel::setDefaultUseMasterKey(true);
Supported relations are:
belongsTo
and its complementhasMany
belongsToMany
, which stores parents ids in an array, and its complementhasManyArray
Please check the tests for examples on relations.
- GrahamCampbell's Laravel-Parse
- HipsterJazzbo's LaraParse
MIT