firevel/firestore-mirror

Mirror Laravel model inside Firestore collection.

0.0.3 2022-03-12 14:45 UTC

This package is auto-updated.

Last update: 2024-04-09 20:39:13 UTC


README

This package can be used to store copy of Laravel model inside Firestore collection.

Installation

Install package:

composer require firevel/firestore-mirror

Add trait \Firevel\FirestoreMirror\HasFirestoreMirror; to the model you would like to mirror.

Configuration

Collection

By default model will be stored inside collection matching model table name. Use $firestoreCollection to customize collection name, for example:

    /**
     * Firestore collection name.
     *
     * @var string
     */
    public $firestoreCollection = 'users';

Create public function getFirestoreCollectionName() method to customize collection name (by default table name).

Document

Create toFirestoreDocument method to customize document schema. By default:

    /**
     * Convert model to firestore document.
     *
     * @return array
     */
    public function toFirestoreDocument()
    {
        return $this->attributesToArray();
    }

Create public function getFirestoreDocumentId() method to customize document id. By default:

    /**
     * Get document id used for mirroring.
     *
     * @return mixed
     */
    public function getFirestoreDocumentId()
    {
        return $this->getKey();
    }