RouteKeyMethodToRouteKeyAttributeRector

Changes model getRouteKeyName() method to use the RouteKey attribute

 use Illuminate\Database\Eloquent\Model;

+#[\Illuminate\Database\Eloquent\Attributes\RouteKey('slug')]
 class Post extends Model
 {
-    public function getRouteKeyName(): string
-    {
-        return 'slug';
-    }
 }

Configure your rector.php:

<?php

use RectorLaravel\Rector\Class_\RouteKeyMethodToRouteKeyAttributeRector;
use Rector\Config\RectorConfig;

return RectorConfig::configure()
    ->withRules([
        RouteKeyMethodToRouteKeyAttributeRector::class,
    ]);
PACKAGE:  laravel/framework >=13.21