2

您好,我正在使用 Laravel-activitylog,当我尝试将火车添加到模型时,它给了我这个错误

App\Models\Setting 类包含 1 个抽象方法,因此必须声明为抽象方法或实现其余方法 (App\Models\Setting::getActivitylogOptions)

这是我所有的模型代码

    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();
    }
}
4

1 回答 1

5

看起来您使用的是 laravel-activitylog 的第 4 版,在这种情况下,您应该添加:

use Spatie\Activitylog\LogOptions;

以及getActivitylogOptions()方法,例如:

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

如果您使用的是 PHP 8,请不要忘记声明返回类型:

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

更多信息在这里:https ://spatie.be/docs/laravel-activitylog/v4/advanced-usage/logging-model-events

对于您在代码中使用的格式,您也可以使用以前的版本,在 composer 中表示:“sptie/laravel-activitylog”:“^3”,并遵循此文档:https ://spatie.be/ docs/laravel-activitylog/v3/介绍

于 2021-05-17T06:52:48.130 回答