记录模型事件特征 LogsActivity 不适用于特定模型,return BadMethodCallException()

Logging model event trait LogsActivity not working on specific model, return BadMethodCallException()

您好,我为 laravel 5.4 使用了 spatie/laravel-activitylog 版本 1.16。

实际上我已经使用包中提供的 LogsActivity Trait 来记录模型事件,并且我的大部分模型都被正确记录。除了我在下面创建的一个模型。

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Functions;
use Carbon;
use Spatie\Activitylog\Traits\LogsActivity;

class RoomClassServiceCharge extends Model
{
    use LogsActivity;

    protected $fillable = [
        'room_class_id',
        'service_charge_id',
        'patient_finance_id',
        'default_price'
    ];

    protected static $logAttributes = [
        'service_charge_name',
        'patient_finance_name',
        'default_price'
    ];

    protected $appends = [
        'service_charge_name',
        'patient_finance_name'
    ];

    protected $table = "roomclass_servicecharges";

    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [
        'created_at',
        'updated_at',
    ];

    protected $dates = [
        'created_at',
        'updated_at',
    ];

    // create/update the record from the passed $request
    public function saveFromRequest($request)
    {
        $inputs = $request->all();

        // compose the user data we will create
        $data = [
            'room_class_id' => $inputs['room_class_id'],
            'service_charge_id' => $inputs['service_charge_id'],
            'patient_finance_id' => $inputs['patient_finance_id'],
            'default_price' => $inputs['default_price'],
        ];

        $this->fill($data);

        // insert/update record in db
        $this->save();
    }

    public static function getRoomClassServiceChargeOptions($room_class_id)
    {
        $options = self::where('room_class_id', '=', $room_class_id)
            ->get()
            ->pluck('service_charge_name', 'id')
            ->all();

        return $options;
    }

    public function getServiceChargeNameAttribute()
    {
        return $this->serviceCharge->name ?? '';
    }

    public function getPatientFinanceNameAttribute()
    {
        return $this->patientFinance->name ?? '';
    }

    public function serviceCharge()
    {
        return $this->belongsTo('App\Models\ServiceCharge');
    }

    public function roomClass()
    {
        return $this->belongsTo('App\Models\RoomClass');
    }

    public function patientFinance()
    {
        return $this->belongsTo('App\Models\PatientFinance');
    }

    protected static function boot()
    {
        parent::boot();
    }

}

每当执行任何 CRUD 操作时,它都会 return BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::getFullnameAttribute() 错误。我不确定它的原因是什么,但我认为这个包可能挂在其他模型上,因为 getFullnameAttribute 存在于不同的模型中,或者可能是因为这个模型名称和 table 与 pivot table。下面提供了完整的错误日志

[2018-10-03 00:43:12] local.ERROR: BadMethodCallException: Call to undefined method Illuminate\Database\Query\Builder::getFullnameAttribute() in /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Query/Builder.php:2445
Stack trace:
#0 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php(1254): Illuminate\Database\Query\Builder->__call('getFullnameAttr...', Array)
#1 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(1374): Illuminate\Database\Eloquent\Builder->__call('getFullnameAttr...', Array)
#2 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(432): Illuminate\Database\Eloquent\Model->__call('getFullnameAttr...', Array)
#3 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(444): Illuminate\Database\Eloquent\Model->mutateAttribute('fullname', NULL)
#4 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(102): Illuminate\Database\Eloquent\Model->mutateAttributeForArray('fullname', NULL)
#5 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(900): Illuminate\Database\Eloquent\Model->attributesToArray()
#6 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php(230): Illuminate\Database\Eloquent\Model->toArray()
#7 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(900): Illuminate\Database\Eloquent\Model->relationsToArray()
#8 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Support/Collection.php(1616): Illuminate\Database\Eloquent\Model->toArray()
#9 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Support/Collection.php(46): Illuminate\Support\Collection->getArrayableItems(Object(App\Models\RoomClassServiceCharge))
#10 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Support/helpers.php(393): Illuminate\Support\Collection->__construct(Object(App\Models\RoomClassServiceCharge))
#11 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/spatie/laravel-activitylog/src/Traits/DetectsChanges.php(79): collect(Object(App\Models\RoomClassServiceCharge))
#12 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/spatie/laravel-activitylog/src/Traits/DetectsChanges.php(50): App\Models\RoomClassServiceCharge::logChanges(Object(App\Models\RoomClassServiceCharge))
#13 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/spatie/laravel-activitylog/src/Traits/LogsActivity.php(37): App\Models\RoomClassServiceCharge->attributeValuesToBeLogged('deleted')
#14 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(348): App\Models\RoomClassServiceCharge::Spatie\Activitylog\Traits\{closure}(Object(App\Models\RoomClassServiceCharge))
#15 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(199): Illuminate\Events\Dispatcher->Illuminate\Events\{closure}('eloquent.delete...', Array)
#16 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Events/Dispatcher.php(172): Illuminate\Events\Dispatcher->dispatch('eloquent.delete...', Array, false)
#17 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Concerns/HasEvents.php(148): Illuminate\Events\Dispatcher->fire('eloquent.delete...', Object(App\Models\RoomClassServiceCharge))
#18 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php(758): Illuminate\Database\Eloquent\Model->fireModelEvent('deleted', false)
#19 /mnt/c/Users/reyna/Workspaces/Projects/simrs/app/Http/Controllers/RoomClassServiceChargeController.php(73): Illuminate\Database\Eloquent\Model->delete()
#20 [internal function]: App\Http\Controllers\RoomClassServiceChargeController->remove('16')
#21 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Controller.php(55): call_user_func_array(Array, Array)
#22 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php(44): Illuminate\Routing\Controller->callAction('remove', Array)
#23 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Routing\ControllerDispatcher->dispatch(Object(Illuminate\Routing\Route), Object(App\Http\Controllers\RoomClassServiceChargeController), 'remove')
#24 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Route.php(160): Illuminate\Routing\Route->runController()
#25 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(572): Illuminate\Routing\Route->run()
#26 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Routing\Router->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#27 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php(41): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#28 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Routing\Middleware\SubstituteBindings->handle(Object(Illuminate\Http\Request), Object(Closure))
#29 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#30 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Auth/Middleware/Authenticate.php(43): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#31 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Auth\Middleware\Authenticate->handle(Object(Illuminate\Http\Request), Object(Closure))
#32 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#33 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/VerifyCsrfToken.php(65): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#34 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\VerifyCsrfToken->handle(Object(Illuminate\Http\Request), Object(Closure))
#35 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#36 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/View/Middleware/ShareErrorsFromSession.php(49): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#37 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\View\Middleware\ShareErrorsFromSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#38 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#39 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Session/Middleware/StartSession.php(64): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#40 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Session\Middleware\StartSession->handle(Object(Illuminate\Http\Request), Object(Closure))
#41 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#42 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/AddQueuedCookiesToResponse.php(37): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#43 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse->handle(Object(Illuminate\Http\Request), Object(Closure))
#44 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#45 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Cookie/Middleware/EncryptCookies.php(59): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#46 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Cookie\Middleware\EncryptCookies->handle(Object(Illuminate\Http\Request), Object(Closure))
#47 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#48 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#49 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(574): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#50 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(533): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#51 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Router.php(511): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#52 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(176): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#53 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(30): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#54 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/barryvdh/laravel-debugbar/src/Middleware/Debugbar.php(51): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#55 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Barryvdh\Debugbar\Middleware\Debugbar->handle(Object(Illuminate\Http\Request), Object(Closure))
#56 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#57 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#58 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#59 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#60 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/TransformsRequest.php(30): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#61 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\TransformsRequest->handle(Object(Illuminate\Http\Request), Object(Closure))
#62 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#63 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/ValidatePostSize.php(27): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#64 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\ValidatePostSize->handle(Object(Illuminate\Http\Request), Object(Closure))
#65 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#66 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#67 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(148): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#68 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#69 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(102): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#70 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(151): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#71 /mnt/c/Users/reyna/Workspaces/Projects/simrs/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#72 /mnt/c/Users/reyna/Workspaces/Projects/simrs/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#73 {main}

任何有关此问题的见解或建议将不胜感激

据我从堆栈跟踪中读取的信息,此问题与 RoomClassServiceCharge class 或 spatie/activity-log 没有直接关系。 (参见堆栈跟踪行 #7)

是否有任何相关的 class 具有 $appends 属性?

此处列出的属性附加到对象的数组表示中。有关详细信息,请参阅 https://laravel.com/docs/5.7/eloquent-serialization#appending-values-to-json

查看与 RoomClassServiceCharge 相关的 classes,某处是 $appends 定义但没有所需的访问函数。