Laravel-activitylog: subject_id NULL when saving data on Pivot table

Created on 25 Sep 2019  路  5Comments  路  Source: spatie/laravel-activitylog

I'm facing a null subject_id when saving data on pivot tables.
This is my extended pivot table model:

namespace App;

use Illuminate\Database\Eloquent\Relations\Pivot;
use Spatie\Activitylog\Traits\LogsActivity;
use Spatie\Activitylog\Contracts\Activity;

class MaterialWork extends Pivot
{
    use LogsActivity;

    protected $table    = 'material_work';
    protected $fillable = ['qty', 'width', 'height', 'price', 'total', 'work_id', 'material_id'];
    protected $dates = ['created_at','updated_at','deleted_at'];

    // Activity Log
    protected static $logFillable = true;
    protected static $submitEmptyLogs = false;
    protected static $logOnlyDirty = true;

    public function tapActivity(Activity $activity, string $eventName)
    {
        $activity->parent_id = $this->work_id;
    }
}

This is the migration file:

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class CreateMaterialWorkTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('material_work', function (Blueprint $table) {
            $table->increments('id');
            $table->integer('qty');
            $table->decimal('width',6,2)->nullable();
            $table->decimal('height',6,2)->nullable();
            $table->decimal('price', 10, 2)->nullable();
            $table->decimal('total', 10, 2);

            $table->integer('work_id')->unsigned();
            $table->integer('material_id')->unsigned();

            $table->timestamps();
            $table->softDeletes();
        });

        Schema::table('material_work', function($table) {
            // Foreign keys
            $table->foreign('work_id')->references('id')->on('works')->onDelete('cascade');
            $table->foreign('material_id')->references('id')->on('materials');
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('material_work');
    }
}

This is how I manually insert a record in the Pivot table:

MaterialWork::updateOrCreate(['id'=>isset($material['mw_id']) ? $material['mw_id'] : null], $material);

subject_id is working fine when updating the Pivot record, but no luck when creting a new one.
Using tapActivity() with a hardcoded subject_id value also works fine, but using something like this doesn't work:

$activity->subject_id = $this->id;

I'm guessing that's something related to the creation order, maybe log is inserted before Pivot record?

Thanks in advance

question

All 5 comments

Hey,

the log is created on created event of the model the trait is attached to.
https://github.com/spatie/laravel-activitylog/blob/89bbdbea8f085d72ba7513af989e2e54366f5794/src/Traits/LogsActivity.php#L21-L22
https://github.com/spatie/laravel-activitylog/blob/89bbdbea8f085d72ba7513af989e2e54366f5794/src/Traits/LogsActivity.php#L107-L111

And to set the subject relation we use the eloquent associate() method.
https://github.com/spatie/laravel-activitylog/blob/89bbdbea8f085d72ba7513af989e2e54366f5794/src/ActivityLogger.php#L50-L55

Could you add a dd() to the last referenced method performedOn()? It should also be fine to add the dd() to your tapActivity() method.

dd($model->getKey());

And create a new entry? It should dump the ID, if set. If the ID is correct (what you expect) the error is related to the eloquent method. If it's null the problem is somewhere else.

I've added some Logs as per your recommendation.


for laravel-activitylog/src/ActivityLogger.php

\Log::debug(.$model->getKey()); returns

{"qty":1,"width":"32","height":"12","price":"32","work_id":953,"material_id":5,"total":32,"updated_at":"2019-09-25 09:27:04","created_at":"2019-09-25 09:27:04"}

\Log::debug(.$model->getKey()); returns empty


for my tapActivity()

\Log::debug($this); returns

{"qty":1,"width":"4","height":"1","price":"22","work_id":953,"material_id":28,"total":22,"updated_at":"2019-09-25 09:20:13","created_at":"2019-09-25 09:20:13"}

\Log::debug($activity); returns

{"log_name":"default","properties":{"attributes":{"qty":1,"width":"4","height":"1","price":"22","total":22,"work_id":953,"material_id":28}},"causer_id":1,"causer_type":"App\\User","subject_id":null,"subject_type":"App\\MaterialWork","parent_id":953,"causer":{"id":1,"name":"Luciano Bosco","email":"[email protected]","email_verified_at":null,"theme_style":"light","active":1,"created_at":"2019-08-09 12:33:19","updated_at":"2019-08-09 12:33:19","deleted_at":null,"permissions":[],"roles":[{"id":1,"name":"developer","title":"Desarrollador","guard_name":"api","created_at":"2019-09-04 20:01:17","updated_at":"2019-09-04 20:01:17","pivot":{"model_id":1,"role_id":1,"model_type":"App\\User"}}]},"subject":{"qty":1,"width":"4","height":"1","price":"22","work_id":953,"material_id":28,"total":22,"updated_at":"2019-09-25 09:20:13","created_at":"2019-09-25 09:20:13"}}  

Ok, no log entry has an id and I think I know what's wrong. You extend the Pivot class which sets $incrementing = false. If you want it to set the id after create you have to set it to true. This should solve it.

Awesome! Thank you so much @Gummibeer , that definitely solved the issue!

Thanks @Gummibeer!
I think this info should be added somewhere in the docs, maybe in this page: https://docs.spatie.be/laravel-activitylog/v3/advanced-usage/logging-model-events/

Was this page helpful?
0 / 5 - 0 ratings