fix: enable queued jobs to create portfolios with owners

This commit is contained in:
hackerESQ
2024-10-24 15:59:54 -05:00
parent 81ed440404
commit 11ae07d69f
2 changed files with 13 additions and 3 deletions
+11 -2
View File
@@ -22,9 +22,18 @@ class Portfolio extends Model
'wishlist',
];
public static ?string $owner_id = null;
protected static function boot()
{
parent::boot();
static::creating(function ($portfolio) {
// enable queued jobs to create portfolios with owners
static::$owner_id = auth()->user()?->id ?? $portfolio->attributes['owner_id'];
unset($portfolio->owner_id);
});
static::saved(function ($portfolio) {
@@ -99,10 +108,10 @@ class Portfolio extends Model
{
// make sure we don't remove owner access
if (!$portfolio->owner_id) {
$users[auth()->user()->id] = ['owner' => true];
$owner[static::$owner_id ?? auth()->user()->id] = ['owner' => true];
// save
$portfolio->users()->sync($users);
$portfolio->users()->sync($owner);
}
}