mongodb / laravel-mongodb

A MongoDB based Eloquent model and Query builder for Laravel (Moloquent)
https://www.mongodb.com/docs/drivers/php/laravel-mongodb/
MIT License
7k stars 1.43k forks source link

Saving & Deleting events not working #2436

Open kemalkanok opened 2 years ago

kemalkanok commented 2 years ago

Description:

Hi there, Whenever i try to use saving and deleting methods i coudn't make it work.

Code:

`

public function saving($model)
{
   dd($model);
}

public function updating($model)
{
   dd($model);
}

public function deleting($model)
{

      dd($model);
}

`

Steps to reproduce

1.Create an observer 2.Try using saving method 3.Try using deleting method

Expected behaviour

  1. Saving event should catch the model instance before updating 2.Deleting event should catch the model instance before deleting

Actual behaviour

1.Saving catches the model instance after updating 2.Deleting event never catches any event.

Logs: [Log](https://user-images.githubusercontent.com/1236890/185644537-09f7ea93-612d-47f2-98d5-dc28a22fb0cc.png)
vinslav commented 2 years ago

I use

public function saving(Brand $brand) {
    $brand->{BrandContract::FIELD_SLUG} = Str::slug($brand->{BrandContract::FIELD_NAME});
}

Works great

lechuhuuha commented 1 year ago

still have error in
"jenssegers/mongodb": "3.8.4", Laravel Framework 8.83.25 "mongodb/mongodb": "^1.12",


class TrackingLog extends Model
{
    /**
     * Bootstrap any application services.
     */
    public static function boot()
    {
        parent::boot();

        static::deleting(function ($item) {
            dd("deltee");
        });
    }

    public function clickLogs()
    {
        return $this->hasMany('EMA\Model\MongoDb\ClickLog', 'message_id', 'message_id');
    }
}

class ClickLog extends Model
{
   public static function boot()
   {
    /**
     * Associations.
     *
     * @var object | collect
     */
    public function trackingLog()
    {
        //return $this->belongsTo('EMA\Model\TrackingLog', 'message_id', 'message_id');
        return $this->belongsTo('EMA\Model\MongoDb\TrackingLog', 'message_id', 'message_id');
    }
   }
}```