laravel-8activitylog

class xxx contains 1 abstract method and must therefore be declared abstract or implement the remaining methods App\Models\xxx::getActivitylogOptions


Hello I am using Laravel-activitylog and when i tried to add the train to Model it gives me this error

Class App\Models\Setting contains 1 abstract method and must therefore be declared abstract or implement the remaining methods (App\Models\Setting::getActivitylogOptions)

and this is all my Model code

    namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Astrotomic\Translatable\Translatable;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Astrotomic\Translatable\Contracts\Translatable as TranslatableContract;
use Spatie\Activitylog\Traits\LogsActivity;

class Setting extends Model
{
    use Translatable,SoftDeletes;
    use LogsActivity;

    

    public $translatedAttributes = ['name', 'slogan', 'description', 'summary', 'address'];
    protected $fillable = ['id', 'logo', 'favicon', 'phone', 'email', 'facebook', 'twitter', 'instagram', 'created_at', 'updated_at'];

    // protected static $logAttributes = ['name', 'logo'];



    public static  function check()
    {
        $setting = Self::all();
        if(count($setting)<1)
        {
            $arrayName = array();
            $arrayName['id']= 1;

            foreach(config("app.languages") as $key => $language)
            {
                $arrayName[$key]['name'] = $language;
                $arrayName[$key]['description'] = $language;
            }
            Self::create($arrayName);
        }
        return $setting = Self::where('id','1')->first();
    }
}

Solution

  • It looks like you are using version 4 of laravel-activitylog, in that case you should add:

    use Spatie\Activitylog\LogOptions;
    

    And the getActivitylogOptions() method, for example:

     public function getActivitylogOptions()
     {
         return LogOptions::defaults()
             -> logOnly(['text'])
             -> logOnlyDirty()
             -> dontSubmitEmptyLogs();
     }
    

    If you are using PHP 8 do not forget to also declare the return type:

    public function getActivitylogOptions(): LogOptions
    {
        return LogOptions::defaults();
    }
    

    More info here: https://spatie.be/docs/laravel-activitylog/v4/advanced-usage/logging-model-events

    For the format you are using in your code to work, you can also use the previous version, indicating in composer: "spatie/laravel-activitylog": "^3", and following this documentation: https://spatie.be/docs/laravel-activitylog/v3/introduction