mardi 2 octobre 2018

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

Hi i have used spatie/laravel-activitylog version 1.16 for laravel 5.4.

Practically i have used LogsActivity Trait provided in the package to logging model event, and most of my model are logged correctly. Except one model that i have created below.

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

}

Whenever any CRUD operation are executed it will return BadMethodCallException Call to undefined method Illuminate\Database\Query\Builder::getFullnameAttribute() error. I am not sure what the cause of it but i think the package might be hook on other model because getFullnameAttribute is exist in different model or maybe its because this model name and table is similar with pivot table. Complete error log are provided below

[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}

Any insight or suggestion regarding this issue would be gladly appreciated



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire