fix:optimize portfolio_users query
This commit is contained in:
@@ -107,7 +107,12 @@ class Portfolio extends Model
|
||||
|
||||
public function getOwnerAttribute()
|
||||
{
|
||||
return $this->users()->firstWhere('owner', 1);
|
||||
if (!$this->relationLoaded('user')) {
|
||||
|
||||
$this->load('users');
|
||||
}
|
||||
|
||||
return $this->users->where('pivot.owner', true)->first();
|
||||
}
|
||||
|
||||
public static function ensurePortfolioHasOwner(self $portfolio)
|
||||
|
||||
@@ -25,7 +25,11 @@
|
||||
<x-ib-toolbar :title="$portfolio->title">
|
||||
|
||||
@if($portfolio->wishlist)
|
||||
<x-badge value="{{ __('Wishlist') }}" class="badge-secondary mr-3" />
|
||||
<x-badge value="{{ __('Wishlist') }}" title="{{ __('Wishlist') }}" class="badge-secondary mr-3" />
|
||||
@endif
|
||||
|
||||
@if(auth()->user()->id !== $portfolio->owner_id)
|
||||
<x-badge value="{{ $portfolio->owner->name }}" title="{{ __('Owner').': '.$portfolio->owner->name }}" class="badge-secondary badge-outline mr-3" />
|
||||
@endif
|
||||
|
||||
@can('fullAccess', $portfolio)
|
||||
|
||||
@@ -14,6 +14,7 @@ class PortfolioPolicyTest extends TestCase
|
||||
use RefreshDatabase;
|
||||
|
||||
protected $policy;
|
||||
protected $owner;
|
||||
protected $user;
|
||||
protected $portfolio;
|
||||
|
||||
@@ -23,12 +24,12 @@ class PortfolioPolicyTest extends TestCase
|
||||
|
||||
$this->policy = new PortfolioPolicy();
|
||||
|
||||
$this->user = User::factory()->create();
|
||||
|
||||
Auth::login($this->user);
|
||||
$this->owner = User::factory()->create();
|
||||
Auth::login($this->owner);
|
||||
$this->portfolio = Portfolio::factory()->create();
|
||||
|
||||
// Attach the users to the portfolio
|
||||
$this->user = User::factory()->create();
|
||||
$this->portfolio->users()->syncWithoutDetaching([
|
||||
$this->user->id => [
|
||||
'full_access' => false,
|
||||
@@ -37,7 +38,7 @@ class PortfolioPolicyTest extends TestCase
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_stranger_access_viaweb()
|
||||
public function test_stranger_access_via_web()
|
||||
{
|
||||
$user = User::factory()->create();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user