Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NO Causer Id when use deleted #597

Closed
mangao05 opened this issue Sep 24, 2019 · 10 comments
Closed

NO Causer Id when use deleted #597

mangao05 opened this issue Sep 24, 2019 · 10 comments

Comments

@mangao05
Copy link

mangao05 commented Sep 24, 2019

this is my Product Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Spatie\Activitylog\Traits\LogsActivity;

class Products extends Model
{
    use SoftDeletes, LogsActivity;

    protected $guarded = [];
    protected static $logUnguarded = true;
    public function subcategory()
    {
        return $this->belongsTo(SubCategory::class, 'subcategory_id', 'id');
    }

    public function product_details(){
        return $this->hasMany(ProductDetails::class, 'product_id', 'id');
    }

    public function product_images(){
        return $this->hasMany(ProductImages::class, 'product_id', 'id');
    }

    public function order_details(){
        return $this->hasMany(OrderDetails::class);
    }
}

this is my delete function
$products->product_details()->delete();

Screenshot_15

@Gummibeer
Copy link
Collaborator

Hey,

first of all the model that gets deleted is ProductDetails not Product. But you also don't delete the model via model method but the DB row via query builder.
That's why no event is triggered and the package can't log an activity.
The difference is if you use your relationship method as method or property.
If you want to log activity for every detail you have to use:

$product->product_details->each->delete();

Or you log it as a single activity and use a custom activity via activity() helper function.

@mangao05
Copy link
Author

mangao05 commented Sep 24, 2019

Hey,

first of all the model that gets deleted is ProductDetails not Product. But you also don't delete the model via model method but the DB row via query builder.
That's why no event is triggered and the package can't log an activity.
The difference is if you use your relationship method as method or property.
If you want to log activity for every detail you have to use:

$product->product_details->each->delete();

Or you log it as a single activity and use a custom activity via activity() helper function.

oh sorry sir, I've put the wrong code. This is my delete for the product.

$product = Products::findOrfail($id);
$product->delete();

@Gummibeer
Copy link
Collaborator

And do you call it in a controller with a logged in user? Or somewhere in a queue, console command?

@mangao05
Copy link
Author

And do you call it in a controller with a logged in user? Or somewhere in a queue, console command?

Actually sir, I'm using the default function whereas I'm just using the LogsActivity in the model. It's working in other function aside from delete. It is saving but no causer_id .

Screenshot_16

@mangao05
Copy link
Author

And do you call it in a controller with a logged in user? Or somewhere in a queue, console command?

Actually sir, I'm using the default function whereas I'm just using the LogsActivity in the model. It's working in other function aside from delete. It is saving but no causer_id .

Screenshot_16

as you can see sir, didnt work in updated too when i Update the user,

@Gummibeer
Copy link
Collaborator

I meant do you call the delete() or update() method in your controller/web runtime or in console one? And if someone is really logged in?

protected function getActivity(): ActivityContract
{
if (! $this->activity instanceof ActivityContract) {
$this->activity = ActivitylogServiceProvider::getActivityModelInstance();
$this
->useLog($this->defaultLogName)
->withProperties([])
->causedBy($this->auth->guard($this->authDriver)->user());
}
return $this->activity;
}

This method is responsible to set the default causer. If you add a dd($this->auth->guard($this->authDriver)->user()); in the if you can see which user is resolved by the used guard.
If it's null there the problem is in your auth procedure.

@mangao05
Copy link
Author

I meant do you call the delete() or update() method in your controller/web runtime or in console one? And if someone is really logged in?

protected function getActivity(): ActivityContract
{
if (! $this->activity instanceof ActivityContract) {
$this->activity = ActivitylogServiceProvider::getActivityModelInstance();
$this
->useLog($this->defaultLogName)
->withProperties([])
->causedBy($this->auth->guard($this->authDriver)->user());
}
return $this->activity;
}

This method is responsible to set the default causer. If you add a dd($this->auth->guard($this->authDriver)->user()); in the if you can see which user is resolved by the used guard.
If it's null there the problem is in your auth procedure.

I call it in my Controller sir.

@spatie-bot
Copy link

Dear contributor,

because this issue seems to be inactive for quite some time now, I've automatically closed it. If you feel this issue deserves some attention from my human colleagues feel free to reopen it.

@vipertecpro
Copy link

ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh i get it now ....so i was using api to create and update from salesforce which doesn't come under auth that's way i was getting causer_type and causer_id null.
So in my case i am using api so how can i set default causer_type and causer_id in my Model.
Please help me. THANK YOU.

@madurapa
Copy link

ohhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh i get it now ....so i was using api to create and update from salesforce which doesn't come under auth that's way i was getting causer_type and causer_id null. So in my case i am using api so how can i set default causer_type and causer_id in my Model. Please help me. THANK YOU.

you can set the logged-in user as the cause

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants