fix: Use slug for Document route model binding

- Add getRouteKeyName() to return 'slug' for Document model
- Add resolveRouteBinding() to support both slug and ID lookups
- Update QuickSwitcher to use slug instead of ID
- Update quick-switcher blade to use slug in routes

This ensures URLs use readable slugs (e.g., /documents/home)
while maintaining backwards compatibility with ID-based URLs
This commit is contained in:
2025-11-29 17:22:17 +09:00
parent 893d3c7a69
commit ac56889a87
3 changed files with 35 additions and 4 deletions

View File

@@ -93,9 +93,9 @@ public function selectDocument()
if (isset($results[$this->selectedIndex])) { if (isset($results[$this->selectedIndex])) {
$document = $results[$this->selectedIndex]; $document = $results[$this->selectedIndex];
// id が存在することを確認 // slug が存在することを確認
if (!empty($document['id'])) { if (!empty($document['slug'])) {
return $this->redirect(route('documents.show', $document['id'])); return $this->redirect(route('documents.show', $document['slug']));
} }
} }
} }

View File

@@ -19,6 +19,37 @@ class Document extends Model
{ {
use SoftDeletes; use SoftDeletes;
/**
* Get the route key for the model.
*
* @return string
*/
public function getRouteKeyName(): string
{
return 'slug';
}
/**
* Retrieve the model for a bound value.
* Supports both slug and ID for backwards compatibility.
*
* @param mixed $value
* @param string|null $field
* @return \Illuminate\Database\Eloquent\Model|null
*/
public function resolveRouteBinding($value, $field = null)
{
// First try to find by slug
$document = $this->where('slug', $value)->first();
// If not found by slug, try by ID (for backwards compatibility)
if (!$document && is_numeric($value)) {
$document = $this->where('id', $value)->first();
}
return $document;
}
/** /**
* The attributes that are mass assignable. * The attributes that are mass assignable.
* *

View File

@@ -68,7 +68,7 @@ class="w-full pl-10 pr-4 py-3 border-0 focus:ring-0 text-lg"
@foreach($this->results as $index => $result) @foreach($this->results as $index => $result)
<li> <li>
<a <a
href="{{ route('documents.show', $result['id']) }}" href="{{ route('documents.show', $result['slug']) }}"
class="block px-4 py-3 hover:bg-gray-50 transition {{ $index === $selectedIndex ? 'bg-indigo-50' : '' }}" class="block px-4 py-3 hover:bg-gray-50 transition {{ $index === $selectedIndex ? 'bg-indigo-50' : '' }}"
wire:navigate wire:navigate
@click="open = false" @click="open = false"