user5880557
user5880557

Reputation:

Configure Laravel project to update and delete JSON objects

I am using Firebase and MySQL as the backend. I can insert data to Firebase using a laravel project. But deleting and updating operations is only valid for MySQL.

I want to make those features active for Firebase also. I am using Mpociot\Firebase\SyncsWithFirebase to sync data with Firebase. It is perfectly working for insertion only. I need a solution for that guys.

Upvotes: 0

Views: 918

Answers (2)

Ruffo Maker
Ruffo Maker

Reputation: 1

You have to get the first record or you won't have the EventCal model instance.

App\EventCal::where('id', 20)->first()->delete();

Upvotes: 0

Ohgodwhy
Ohgodwhy

Reputation: 50787

There's nothing special that needs to be done. It handles it for you. let's have a look at that trait

public static function bootSyncsWithFirebase()
{
    static::created(function ($model) {
        $model->saveToFirebase('set');
    });
    static::updated(function ($model) {
        $model->saveToFirebase('update');
    });
    static::deleted(function ($model) {
        $model->saveToFirebase('delete');
    });
}

OK. So it attaches its self to the create/update/delete events. What do the rest of the functions do?

protected function saveToFirebase($mode)
{
    if (is_null($this->firebaseClient)) {
        $this->firebaseClient = new FirebaseLib(config('services.firebase.database_url'), config('services.firebase.secret'));
    }
    $path = $this->getTable() . '/' . $this->getKey();
    if ($mode === 'set' && $fresh = $this->fresh()) {
        $this->firebaseClient->set($path, $fresh->toArray());
    } elseif ($mode === 'update' && $fresh = $this->fresh()) {
        $this->firebaseClient->update($path, $fresh->toArray());
    } elseif ($mode === 'delete') {
        $this->firebaseClient->delete($path);
    }
}

There it is. When a delete occurs through Eloquent/FQB, it's already deleted in Firebase.

Your issue may be that you're not using Eloquent to perform your delete/udpates, so no event is received.

Upvotes: 1

Related Questions