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
}
// 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();
class User extends Model
{
use HasFirestoreMirror;
public function shouldMirrorToFirestore()
{
return $this->is_active && $this->email_verified_at !== null;
}
public function mirrorToFirestore()
{
if ($this->shouldMirrorToFirestore()) {
return parent::mirrorToFirestore();
}
return $this;
}
}
class Order extends Model
{
use HasFirestoreMirror;
public function getFirestoreCollectionName()
{
return "tenants/{$this->tenant_id}/orders";
}
}