loads of progress
This commit is contained in:
@@ -3,6 +3,7 @@
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
|
||||
class Exercise extends Model
|
||||
{
|
||||
@@ -44,4 +45,25 @@ class Exercise extends Model
|
||||
{
|
||||
return $this->belongsToMany(User::class, 'exercise_favorites')->withTimestamps();
|
||||
}
|
||||
|
||||
public function aliases()
|
||||
{
|
||||
return $this->hasMany(ExerciseAlias::class);
|
||||
}
|
||||
|
||||
public function getDisplayNameAttribute(): string
|
||||
{
|
||||
$name = (string) $this->attributes['name'] ?? '';
|
||||
$userId = Auth::id();
|
||||
if (!$userId) {
|
||||
return $name;
|
||||
}
|
||||
// Use already loaded relation if present to avoid querying N+1
|
||||
if ($this->relationLoaded('aliases')) {
|
||||
$alias = $this->aliases->firstWhere('user_id', $userId);
|
||||
return $alias?->name ?: $name;
|
||||
}
|
||||
$alias = $this->aliases()->where('user_id', $userId)->first();
|
||||
return $alias?->name ?: $name;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user