Back

Extending spatie laravel-activitylogs Activity Model and add


I'm working on Laravel 5.4 application and trying to install spatie/laravel-activitylog v1.16 (old version since I'm not sure if the newer v2.4.3 will working with 5.4) link to package

My problem is the package says I can add a causer using the package CausesActivity Trait. I added it to the Company model because every model has a company_id attribute but that didn't work. I have a Company with many users would like to add the company id to the table on creating a log. I created a CompanyLogActivity model that extends the Activity Model and added company_id column to the activity_log table from the (package) and added the new model to the config file.

I then tried:

Adding belongsToTenant Trait with no use

use Illuminate\Database\Eloquent\Model;  
use Spatie\Activitylog\Models\Activity;  
use HipsterJazzbo\Landlord\BelongsToTenants;   
class CompanyActivityLogs extends Activity  
{  
    use BelongsToTenants;  
    protected $table = ['activity_log'];  
    public $tenantColumns = ['company_id'];  
 }    

Using $fillable on the model and observing for creating or saving to fill company_id

<?php

namespace App\Observers;  
use App\CompanyActivityLogs;  
use Auth;  
use Spatie\Activitylog\Models\Activity;  
class CompanyActivity  
{  
public function creating($model)  
{  
    $model->fill(['company_id' => Auth::user()->company_id])->save();  
}  
public function updating($model) {  
    $model->fill(['company_id' => Auth::user()->company_id])->save();  
}  

Then in my new CompanyActivityLogs model

public static function boot() {  
 parent::boot();  
CompanyActivityLogs::observe(new CompanyActivity()); 
}

That didn't work and I thought was gonna be a great solution. I tried switching out CompanyActivityLogs for Activity (the parent model ) in the observe statement and that didn't work.

I tried to add the to the extended model

public static function boot()
 {
    parent::boot();
    static::creating(function($model)
    {

        $model->created_by = public static function boot()
 {
    parent::boot();
    static::creating(function($model)
    {         
        $model->created_by = Auth::user()->company_id;
        //also tried  $model->save(); here

    });      
};

Lastly I tried appending it to the parent::array

protected $fillable = ['company_id'];  
public function toArray()  
{  
    $array = parent::toArray();  
    foreach ($this->getMutatedAttributes() as $key)  
    {  
        if ( ! array_key_exists($key, $array)) {  
            $array['company_id'] = Auth::user()->company_id;  
        }  
    } 
    return $array;  
}

I tried so many different ways thinking one of them would help.

I have been working with laravel for a year but learn something new every day and have never tried extending a model, which is not as simple as one may think. I do need a solution if anyone has one for me or if someone can tell me where I went wrong. Thanks.


Sign in to participate in this thread!



We'd like to thank these amazing companies for supporting us