1. Go to this page and download the library: Download firevel/firestore-mirror library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
firevel / firestore-mirror example snippets
use Firevel\FirestoreMirror\HasFirestoreMirror;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasFirestoreMirror;
// Optional: Customize the Firestore collection name
public $firestoreCollection = 'users';
// Optional: Customize the document structure
public function toFirestoreDocument()
{
return [
'id' => $this->id,
'name' => $this->name,
'email' => $this->email,
'created_at' => $this->created_at->toIso8601String(),
];
}
}
class User extends Model
{
use HasFirestoreMirror;
public $firestoreCollection = 'app_users';
}
class User extends Model
{
use HasFirestoreMirror;
public function getFirestoreCollectionName()
{
return 'users_' . $this->tenant_id;
}
}
public function getFirestoreDocumentId()
{
return $this->uuid; // Use a UUID field instead
}
class User extends Model
{
use HasFirestoreMirror;
// Mirror to multiple collections
public $firestoreCollection = ['users', 'public_profiles'];
}
class User extends Model
{
use HasFirestoreMirror;
public function getFirestoreCollectionName()
{
return ['users', 'public_profiles', 'search_index'];
}
}
class User extends Model
{
use HasFirestoreMirror;
public function getFirestoreCollectionNames(): array
{
return ['users', 'public_profiles'];
}
}
// This will automatically create a document in Firestore
$user = User::create([
'name' => 'John Doe',
'email' => '[email protected]'
]);
// This will automatically update the document in Firestore
$user->update(['name' => 'Jane Doe']);
// This will automatically delete the document from Firestore
$user->delete();
$user = User::find(1);
$user->mirrorToFirestore(); // Manually sync to Firestore
// Sync all users to Firestore
User::all()->mirrorToFirestore();
// Sync filtered collections
User::where('active', true)->get()->mirrorToFirestore();
// Sync users with relationships loaded
User::with('posts')->get()->mirrorToFirestore();
// Sync paginated results
User::paginate(100)->mirrorToFirestore();
// Disable syncing for a single operation
User::withoutSyncingToFirestore(function () {
User::create([
'name' => 'John Doe',
'email' => '[email protected]'
]);
});
// Disable syncing for multiple operations
User::withoutSyncingToFirestore(function () {
// None of these will sync to Firestore
$user = User::create(['name' => 'Jane']);
$user->update(['email' => '[email protected]']);
User::find(1)->update(['status' => 'inactive']);
});
// The callback return value is passed through
$user = User::withoutSyncingToFirestore(function () {
return User::create(['name' => 'Test User']);
});
class User extends Model
{
use HasFirestoreMirror;
/**
* Determine if the model should be mirrored to Firestore.
*
* @return bool
*/
public function shouldMirrorToFirestore()
{
// Only mirror active users with verified emails
return $this->is_active && $this->email_verified_at !== null;
}
}
class Order extends Model
{
use HasFirestoreMirror;
public function getFirestoreCollectionName()
{
return "tenants/{$this->tenant_id}/orders";
}
}