├── composer.json
├── src
└── Mpociot
│ └── Firebase
│ └── SyncsWithFirebase.php
└── README.md
/composer.json:
--------------------------------------------------------------------------------
1 | {
2 | "name": "mpociot/laravel-firebase-sync",
3 | "license": "MIT",
4 | "description": "Synchronize your Eloquent models with a Firebase realtime database.",
5 | "keywords": [
6 | "firebase",
7 | "eloquent",
8 | "laravel"
9 | ],
10 | "homepage": "http://github.com/mpociot/laravel-firebase-sync",
11 | "authors": [
12 | {
13 | "name": "Marcel Pociot",
14 | "email": "m.pociot@gmail.com"
15 | }
16 | ],
17 | "support": {
18 | "issues": "https://github.com/mpociot/laravel-firebase-sync/issues",
19 | "source": "https://github.com/mpociot/laravel-firebase-sync"
20 | },
21 | "require": {
22 | "php": ">=5.4.0",
23 | "illuminate/support": "~5.0",
24 | "ktamas77/firebase-php": "^2.2"
25 | },
26 | "require-dev": {
27 | "mockery/mockery": "~0.9",
28 | "orchestra/testbench": "~3.0",
29 | "phpunit/phpunit": "~4.7 || ~5.0"
30 | },
31 | "autoload": {
32 | "psr-0": {
33 | "Mpociot\\Firebase": "src/"
34 | }
35 | },
36 | "autoload-dev": {
37 | "psr-4": {
38 | "Mpociot\\Firebase\\Tests\\": "tests/"
39 | }
40 | }
41 | }
42 |
--------------------------------------------------------------------------------
/src/Mpociot/Firebase/SyncsWithFirebase.php:
--------------------------------------------------------------------------------
1 | saveToFirebase('set');
27 | });
28 | static::updated(function ($model) {
29 | $model->saveToFirebase('update');
30 | });
31 | static::deleted(function ($model) {
32 | $model->saveToFirebase('delete');
33 | });
34 | static::restored(function ($model) {
35 | $model->saveToFirebase('set');
36 | });
37 | }
38 |
39 | /**
40 | * @param FirebaseInterface|null $firebaseClient
41 | */
42 | public function setFirebaseClient($firebaseClient)
43 | {
44 | $this->firebaseClient = $firebaseClient;
45 | }
46 |
47 | /**
48 | * @return array
49 | */
50 | protected function getFirebaseSyncData()
51 | {
52 | if ($fresh = $this->fresh()) {
53 | return $fresh->toArray();
54 | }
55 | return [];
56 | }
57 |
58 | /**
59 | * @param $mode
60 | */
61 | protected function saveToFirebase($mode)
62 | {
63 | if (is_null($this->firebaseClient)) {
64 | $this->firebaseClient = new FirebaseLib(config('services.firebase.database_url'), config('services.firebase.secret'));
65 | }
66 | $path = $this->getTable() . '/' . $this->getKey();
67 |
68 | if ($mode === 'set') {
69 | $this->firebaseClient->set($path, $this->getFirebaseSyncData());
70 | } elseif ($mode === 'update') {
71 | $this->firebaseClient->update($path, $this->getFirebaseSyncData());
72 | } elseif ($mode === 'delete') {
73 | $this->firebaseClient->delete($path);
74 | }
75 | }
76 | }
77 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Laravel Firebase Sync
2 | ## Synchronize your Eloquent models with the [Firebase Realtime Database](https://firebase.google.com/docs/database/)
3 |
4 | 
5 | 
6 | [](https://codecov.io/github/mpociot/laravel-firebase-sync?branch=master)
7 | [](https://travis-ci.org/mpociot/laravel-firebase-sync)
8 |
9 | ## Contents
10 |
11 | - [Installation](#installation)
12 | - [Usage](#usage)
13 | - [License](#license)
14 |
15 |
16 |
17 | ## Installation
18 |
19 | In order to add Laravel Firebase Sync to your project, just add
20 |
21 | "mpociot/laravel-firebase-sync": "~1.0"
22 |
23 | to your composer.json. Then run `composer install` or `composer update`.
24 |
25 | Or run `composer require mpociot/laravel-firebase-sync ` if you prefer that.
26 |
27 |
28 |
29 |
30 | ## Usage
31 |
32 | ### Configuration
33 |
34 | This package requires you to add the following section to your `config/services.php` file:
35 |
36 | ```php
37 | 'firebase' => [
38 | 'api_key' => 'API_KEY', // Only used for JS integration
39 | 'auth_domain' => 'AUTH_DOMAIN', // Only used for JS integration
40 | 'database_url' => 'https://your-database-at.firebaseio.com',
41 | 'secret' => 'DATABASE_SECRET',
42 | 'storage_bucket' => 'STORAGE_BUCKET', // Only used for JS integration
43 | ]
44 | ```
45 |
46 | **Note**: This package only requires the configuration keys `database_url` and `secret`. The other keys are only necessary if you want to also use the firebase JS API.
47 |
48 | ### Synchronizing models
49 |
50 | To synchronize your Eloquent models with the Firebase realtime database, simply let the models that you want to synchronize with Firebase use the `Mpociot\Firebase\SyncsWithFirebase` trait.
51 |
52 | ```php
53 | use Mpociot\Firebase\SyncsWithFirebase;
54 |
55 | class User extends Model {
56 |
57 | use SyncsWithFirebase;
58 |
59 | }
60 | ```
61 |
62 | The data that will be synchronized is the array representation of your model. That means that you can modify the data using the existing Eloquent model attributes like `visible`, `hidden` or `appends`.
63 |
64 | If you need more control over the data that gets synchronized with Firebase, you can override the `getFirebaseSyncData` of the `SyncsWithFirebase` trait and let it return the array data you want to send to Firebase.
65 |
66 |
67 |
68 |
69 | ## License
70 |
71 | Laravel Firebase Sync is free software distributed under the terms of the MIT license.
72 |
--------------------------------------------------------------------------------