Find the best Rector rule to solve your problem. Searching through 1010 rules.

Found 56 rules:

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';
-    }
 }
PACKAGE:  laravel/framework >=13.21

DelayPropertyToDelayAttributeRector

Changes the delay property to use the Delay attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Delay;

+#[Delay(10)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $delay = 10;
 }
PACKAGE:  laravel/framework >=13.4

WithoutTimestampsPropertyToWithoutTimestampsAttributeRector

Changes model timestamps = false property to use the WithoutTimestamps attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\WithoutTimestamps;

+#[WithoutTimestamps]
 class EventLog extends Model
 {
-    public $timestamps = false;
 }
PACKAGE:  laravel/framework >=13.2

WithoutIncrementingPropertyToWithoutIncrementingAttributeRector

Changes model incrementing = false property to use the WithoutIncrementing attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\WithoutIncrementing;

+#[WithoutIncrementing]
 class User extends Model
 {
-    public $incrementing = false;
 }
PACKAGE:  laravel/framework >=13.2

AliasesPropertyToAliasesAttributeRector

Changes the aliases property to use the Aliases attribute

 use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Aliases;

+#[Aliases(['email:send'])]
 class SendEmails extends Command
 {
-    protected $aliases = ['email:send'];
 }
PACKAGE:  laravel/framework >=13.2

DateFormatPropertyToDateFormatAttributeRector

Changes model dateFormat property to use the DateFormat attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\DateFormat;

+#[DateFormat('U')]
 class User extends Model
 {
-    protected $dateFormat = 'U';
 }
PACKAGE:  laravel/framework >=13.2

VisiblePropertyToVisibleAttributeRector

Changes model visible property to use the Visible attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Visible;

+#[Visible(['name', 'email'])]
 class User extends Model
 {
-    protected $visible = [
-        'name',
-        'email',
-    ];
 }
PACKAGE:  laravel/framework >=13.0

TablePropertyToTableAttributeRector

Changes model table-related properties to use the Table attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Table;

+#[Table(name: 'users', key: 'user_id', keyType: 'string', incrementing: false)]
 class User extends Model
 {
-    protected $table = 'users';
-
-    protected $primaryKey = 'user_id';
-
-    protected $keyType = 'string';
-
-    protected $incrementing = false;
 }
PACKAGE:  laravel/framework >=13.0

FillablePropertyToFillableAttributeRector

Changes model fillable property to use the fillable attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Fillable;

+#[Fillable(['name', 'email'])]
 class User extends Model
 {
-    protected $fillable = [
-        'name',
-        'email',
-    ];
 }
PACKAGE:  laravel/framework >=13.0

EmptyGuardedPropertyToUnguardedAttributeRector

Changes model empty guarded property to use the Unguarded attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Unguarded;

+#[Unguarded]
 class User extends Model
 {
-    protected $guarded = [];
 }
PACKAGE:  laravel/framework >=13.0

GuardedPropertyToGuardedAttributeRector

Changes model guarded property to use the guarded attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Guarded;

+#[Guarded(['is_admin'])]
 class User extends Model
 {
-    protected $guarded = [
-        'is_admin',
-    ];
 }
PACKAGE:  laravel/framework >=13.0

CommandHiddenPropertyToHiddenAttributeRector

Changes the hidden property to use the Hidden attribute on console commands

 use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Hidden;

+#[Hidden]
 class SendEmails extends Command
 {
-    protected $hidden = true;
 }
PACKAGE:  laravel/framework >=13.0

HelpPropertyToHelpAttributeRector

Changes the help property to use the Help attribute

 use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Help;

+#[Help('This command sends emails to all users')]
 class SendEmails extends Command
 {
-    protected $help = 'This command sends emails to all users';
 }
PACKAGE:  laravel/framework >=13.0

AppendsPropertyToAppendsAttributeRector

Changes model appends property to use the appends attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Appends;

+#[Appends(['full_name'])]
 class User extends Model
 {
-    protected $appends = [
-        'full_name',
-    ];
 }
PACKAGE:  laravel/framework >=13.0

BackoffPropertyToBackoffAttributeRector

Changes the backoff property to use the Backoff attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Backoff;

+#[Backoff(3)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $backoff = 3;
 }
PACKAGE:  laravel/framework >=13.0

MaxExceptionsPropertyToMaxExceptionsAttributeRector

Changes the maxExceptions property to use the MaxExceptions attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\MaxExceptions;

+#[MaxExceptions(3)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $maxExceptions = 3;
 }
PACKAGE:  laravel/framework >=13.0

SignaturePropertyToSignatureAttributeRector

Changes the signature property to use the Signature attribute

 use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Signature;

+#[Signature('mail:send {user}')]
 class SendEmails extends Command
 {
-    protected $signature = 'mail:send {user}';
 }
PACKAGE:  laravel/framework >=13.0

TimeoutPropertyToTimeoutAttributeRector

Changes the timeout property to use the Timeout attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Timeout;

+#[Timeout(120)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $timeout = 120;
 }
PACKAGE:  laravel/framework >=13.0

JobConnectionPropertyToJobConnectionAttributeRector

Changes the connection property to use the Connection attribute on queue jobs

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Connection;

+#[Connection('redis')]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $connection = 'redis';
 }
PACKAGE:  laravel/framework >=13.0

CollectsPropertyToCollectsAttributeRector

Changes the collects property to use the Collects attribute

 use App\Http\Resources\UserResource;
+use Illuminate\Http\Resources\Attributes\Collects;
 use Illuminate\Http\Resources\Json\JsonResource;

+#[Collects(UserResource::class)]
 class UserCollection extends JsonResource
 {
-    protected $collects = UserResource::class;
 }
PACKAGE:  laravel/framework >=13.0

QueuePropertyToQueueAttributeRector

Changes the queue property to use the Queue attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Queue;

+#[Queue('podcasts')]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $queue = 'podcasts';
 }
PACKAGE:  laravel/framework >=13.0

FailOnTimeoutPropertyToFailOnTimeoutAttributeRector

Changes the failOnTimeout property to use the FailOnTimeout attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\FailOnTimeout;

+#[FailOnTimeout(true)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $failOnTimeout = true;
 }
PACKAGE:  laravel/framework >=13.0

UniqueForPropertyToUniqueForAttributeRector

Changes the uniqueFor property to use the UniqueFor attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\UniqueFor;

+#[UniqueFor(1800)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $uniqueFor = 1800;
 }
PACKAGE:  laravel/framework >=13.0

ConnectionPropertyToConnectionAttributeRector

Changes model connection property to use the Connection attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Connection;

+#[Connection('sqlite')]
 class User extends Model
 {
-    protected $connection = 'sqlite';
 }
PACKAGE:  laravel/framework >=13.0

TouchesPropertyToTouchesAttributeRector

Changes model touches property to use the touches attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Touches;

+#[Touches(['posts'])]
 class User extends Model
 {
-    protected $touches = [
-        'posts',
-    ];
 }
PACKAGE:  laravel/framework >=13.0

StopOnFirstFailurePropertyToStopOnFirstFailureAttributeRector

Changes the stopOnFirstFailure property to use the StopOnFirstFailure attribute

 use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Foundation\Http\Attributes\StopOnFirstFailure;

+#[StopOnFirstFailure]
 class StorePostRequest extends FormRequest
 {
-    protected $stopOnFirstFailure = true;
 }
PACKAGE:  laravel/framework >=13.0

PreserveKeysPropertyToPreserveKeysAttributeRector

Changes the preserveKeys property to use the PreserveKeys attribute

+use Illuminate\Http\Resources\Attributes\PreserveKeys;
 use Illuminate\Http\Resources\Json\JsonResource;

+#[PreserveKeys]
 class UserResource extends JsonResource
 {
-    protected $preserveKeys = true;
 }
PACKAGE:  laravel/framework >=13.0

HiddenPropertyToHiddenAttributeRector

Changes model hidden property to use the hidden attribute

 use Illuminate\Database\Eloquent\Model;
+use Illuminate\Database\Eloquent\Attributes\Hidden;

+#[Hidden(['password'])]
 class User extends Model
 {
-    protected $hidden = [
-        'password',
-    ];
 }
PACKAGE:  laravel/framework >=13.0

TriesPropertyToTriesAttributeRector

Changes the tries property to use the Tries attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\Tries;

+#[Tries(3)]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $tries = 3;
 }
PACKAGE:  laravel/framework >=13.0

DescriptionPropertyToDescriptionAttributeRector

Changes the description property to use the Description attribute

 use Illuminate\Console\Command;
+use Illuminate\Console\Attributes\Description;

+#[Description('Send marketing emails to users')]
 class SendEmails extends Command
 {
-    protected $description = 'Send marketing emails to users';
 }
PACKAGE:  laravel/framework >=13.0

ErrorBagPropertyToErrorBagAttributeRector

Changes the errorBag property to use the ErrorBag attribute

 use Illuminate\Foundation\Http\FormRequest;
+use Illuminate\Foundation\Http\Attributes\ErrorBag;

+#[ErrorBag('custom')]
 class StorePostRequest extends FormRequest
 {
-    protected $errorBag = 'custom';
 }
PACKAGE:  laravel/framework >=13.0

ScopeNamedClassMethodToScopeAttributedClassMethodRector

Changes model scope methods to use the scope attribute

 class User extends Model
 {
-    public function scopeActive($query)
+    #[\Illuminate\Database\Eloquent\Attributes\Scope]
+    protected function active($query)
     {
         return $query->where('active', 1);
     }
 }
PACKAGE:  laravel/framework >=12.4

ContainerBindConcreteWithClosureOnlyRector

Drop the specified abstract class from the bind method and replace it with a closure that returns the abstract class.

-$this->app->bind(SomeClass::class, function (): SomeClass {
+$this->app->bind(function (): SomeClass {
     return new SomeClass();
 });
PACKAGE:  laravel/framework >=12.0

CollectedByPropertyToCollectedByAttributeRector

Changes model newCollection method to use the CollectedBy attribute

 use Illuminate\Database\Eloquent\Model;
-use Illuminate\Database\Eloquent\Collection;
 use App\Collections\UserCollection;
+use Illuminate\Database\Eloquent\Attributes\CollectedBy;

+#[CollectedBy(UserCollection::class)]
 class User extends Model
 {
-    /**
-     * @param  array<int, \Illuminate\Database\Eloquent\Model>  $models
-     * @return \Illuminate\Database\Eloquent\Collection<int, \Illuminate\Database\Eloquent\Model>
-     */
-    public function newCollection(array $models = []): Collection
-    {
-        return new UserCollection($models);
-    }
 }
PACKAGE:  laravel/framework >=11.28

DeleteWhenMissingModelsPropertyToDeleteWhenMissingModelsAttributeRector

Changes the deleteWhenMissingModels property to use the DeleteWhenMissingModels attribute

 use Illuminate\Contracts\Queue\ShouldQueue;
+use Illuminate\Queue\Attributes\DeleteWhenMissingModels;

+#[DeleteWhenMissingModels]
 final class ProcessPodcast implements ShouldQueue
 {
-    public $deleteWhenMissingModels = true;
 }
PACKAGE:  laravel/framework >=11.3

RefactorBlueprintGeometryColumnsRector

refactors calls with the pre Laravel 11 methods for blueprint geometry columns

-$blueprint->point('coordinates')->spatialIndex();
+$blueprint->geometry('coordinates', 'point')->spatialIndex();
PACKAGE:  laravel/framework >=11.0

AssertSeeToAssertSeeHtmlRector

Replace assertSee with assertSeeHtml when testing HTML with escape set to false

-$response->assertSee("<li>foo</li>", false);
+$response->assertSeeHtml("<li>foo</li>");
PACKAGE:  laravel/framework >=11.0

ReplaceQueueTraitsWithQueueableRector

Replace Dispatchable, InteractsWithQueue, Queueable, and SerializesModels traits with the Queueable trait

 use Illuminate\Bus\Queueable;
 use Illuminate\Foundation\Bus\Dispatchable;
 use Illuminate\Queue\InteractsWithQueue;
 use Illuminate\Queue\SerializesModels;

 class SomeJob
 {
-    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
+    use \Illuminate\Foundation\Queue\Queueable;
 }
PACKAGE:  laravel/framework >=11.0

ModelCastsPropertyToCastsMethodRector

Refactor Model $casts property with casts() method

 use Illuminate\Database\Eloquent\Model;

 class Person extends Model
 {
-    protected $casts = [
-        'age' => 'integer',
-    ];
+    protected function casts(): array
+    {
+        return [
+            'age' => 'integer',
+        ];
+    }
 }
PACKAGE:  laravel/framework >=11.0

DatabaseExpressionToStringToMethodCallRector

Convert DB Expression __toString() calls to getValue() method calls.

 use Illuminate\Support\Facades\DB;

-$string = DB::raw('select 1')->__toString();
+$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar());
PACKAGE:  laravel/framework >=10.0

ReplaceWithoutJobsEventsAndNotificationsWithFacadeFakeRector

Replace withoutJobs, withoutEvents and withoutNotifications with Facade fake

-$this->withoutJobs();
-$this->withoutEvents();
-$this->withoutNotifications();
+\Illuminate\Support\Facades\Bus::fake();
+\Illuminate\Support\Facades\Event::fake();
+\Illuminate\Support\Facades\Notification::fake();
PACKAGE:  laravel/framework >=10.0

DispatchNonShouldQueueToDispatchSyncRector

Dispatch non ShouldQueue jobs to dispatchSync

-dispatch(new SomeJob());
-Bus::dispatch(new SomeJob());
-$this->dispatch(new SomeJob());
+dispatch_sync(new SomeJob());
+Bus::dispatchSync(new SomeJob());
+$this->dispatchSync(new SomeJob());
PACKAGE:  laravel/framework >=10.0

DatabaseExpressionCastsToMethodCallRector

Convert DB Expression string casts to getValue() method calls.

 use Illuminate\Support\Facades\DB;

-$string = (string) DB::raw('select 1');
+$string = DB::raw('select 1')->getValue(DB::connection()->getQueryGrammar());
PACKAGE:  laravel/framework >=10.0

ReplaceAssertTimesSendWithAssertSentTimesRector

Replace assertTimesSent with assertSentTimes

-Notification::assertTimesSent(1, SomeNotification::class);
+Notification::assertSentTimes(SomeNotification::class, 1);
PACKAGE:  laravel/framework >=10.0

UnifyModelDatesWithCastsRector

Unify Model $dates property with $casts

 use Illuminate\Database\Eloquent\Model;

 class Person extends Model
 {
     protected $casts = [
-        'age' => 'integer',
+        'age' => 'integer', 'birthday' => 'datetime',
     ];
-
-    protected $dates = ['birthday'];
 }
PACKAGE:  laravel/framework >=10.0

ReplaceExpectsMethodsInTestsRector

Replace expectJobs and expectEvents methods in tests

 use Illuminate\Foundation\Testing\TestCase;

 class SomethingTest extends TestCase
 {
     public function testSomething()
     {
-        $this->expectsJobs([\App\Jobs\SomeJob::class, \App\Jobs\SomeOtherJob::class]);
-        $this->expectsEvents(\App\Events\SomeEvent::class);
-        $this->doesntExpectEvents(\App\Events\SomeOtherEvent::class);
+        \Illuminate\Support\Facades\Bus::fake([\App\Jobs\SomeJob::class, \App\Jobs\SomeOtherJob::class]);
+        \Illuminate\Support\Facades\Event::fake([\App\Events\SomeEvent::class, \App\Events\SomeOtherEvent::class]);

         $this->get('/');
+
+        \Illuminate\Support\Facades\Bus::assertDispatched(\App\Jobs\SomeJob::class);
+        \Illuminate\Support\Facades\Bus::assertDispatched(\App\Jobs\SomeOtherJob::class);
+        \Illuminate\Support\Facades\Event::assertDispatched(\App\Events\SomeEvent::class);
+        \Illuminate\Support\Facades\Event::assertNotDispatched(\App\Events\SomeOtherEvent::class);
     }
 }
PACKAGE:  laravel/framework >=10.0

ReplaceFakerInstanceWithHelperRector

Replace $this->faker with the fake() helper function in Factories

 class UserFactory extends Factory
 {
     public function definition()
     {
         return [
-            'name' => $this->faker->name,
-            'email' => $this->faker->unique()->safeEmail,
+            'name' => fake()->name,
+            'email' => fake()->unique()->safeEmail,
         ];
     }
 }
PACKAGE:  laravel/framework >=9.0

AddExtendsAnnotationToModelFactoriesRector

Adds the @extends annotation to Factories.

 use Illuminate\Database\Eloquent\Factories\Factory;

+/**
+ * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\User>
+ */
 class UserFactory extends Factory
 {
     protected $model = \App\Models\User::class;
 }
PACKAGE:  laravel/framework >=9.0

MigrateToSimplifiedAttributeRector

Migrate to the new Model attributes syntax

 use Illuminate\Database\Eloquent\Model;

 class User extends Model
 {
-    public function getFirstNameAttribute($value)
-    {
-        return ucfirst($value);
-    }
-
-    public function setFirstNameAttribute($value)
+    protected function firstName(): \Illuminate\Database\Eloquent\Casts\Attribute
     {
-        $this->attributes['first_name'] = strtolower($value);
-        $this->attributes['first_name_upper'] = strtoupper($value);
+        return \Illuminate\Database\Eloquent\Casts\Attribute::make(get: function ($value) {
+            return ucfirst($value);
+        }, set: function ($value) {
+            return ['first_name' => strtolower($value), 'first_name_upper' => strtoupper($value)];
+        });
     }
 }
PACKAGE:  laravel/framework >=9.0

AddParentRegisterToEventServiceProviderRector

Add parent::register(); call to register() class method in child of Illuminate\Foundation\Support\Providers\EventServiceProvider

 use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;

 class EventServiceProvider extends ServiceProvider
 {
     public function register()
     {
+        parent::register();
     }
 }
PACKAGE:  laravel/framework >=8.0

PropertyDeferToDeferrableProviderToRector

Change deprecated $defer = true; to Illuminate\Contracts\Support\DeferrableProvider interface

 use Illuminate\Support\ServiceProvider;
+use Illuminate\Contracts\Support\DeferrableProvider;

-final class SomeServiceProvider extends ServiceProvider
+final class SomeServiceProvider extends ServiceProvider implements DeferrableProvider
 {
-    /**
-     * @var bool
-     */
-    protected $defer = true;
 }
PACKAGE:  laravel/framework >=5.8

ChangeQueryWhereDateValueWithCarbonRector

Refactor whereDate() queries to include both date and time comparisons with Carbon

 use Illuminate\Database\Query\Builder;

 final class SomeClass
 {
     public function run(Builder $query)
     {
-        $query->whereDate('created_at', '<', Carbon::now());
+        $dateTime = Carbon::now();
+        $query->whereDate('created_at', '<=', $dateTime);
+        $query->whereTime('created_at', '<=', $dateTime);
     }
 }
PACKAGE:  laravel/framework >=5.7

Redirect301ToPermanentRedirectRector

Change "redirect" call with 301 to "permanentRedirect"

 class SomeClass
 {
     public function run()
     {
-        Illuminate\Routing\Route::redirect('/foo', '/bar', 301);
+        Illuminate\Routing\Route::permanentRedirect('/foo', '/bar');
     }
 }
PACKAGE:  laravel/framework >=5.7

AddGuardToLoginEventRector

Add new $guard argument to Illuminate\Auth\Events\Login

 use Illuminate\Auth\Events\Login;

 final class SomeClass
 {
     public function run(): void
     {
-        $loginEvent = new Login('user', false);
+        $guard = config('auth.defaults.guard');
+        $loginEvent = new Login($guard, 'user', false);
     }
 }
PACKAGE:  laravel/framework >=5.7

AddMockConsoleOutputFalseToConsoleTestsRector

Add "$this->mockConsoleOutput = false"; to console tests that work with output content

 use Illuminate\Support\Facades\Artisan;
 use Illuminate\Foundation\Testing\TestCase;

 final class SomeTest extends TestCase
 {
+    protected function setUp(): void
+    {
+        parent::setUp();
+
+        $this->mockConsoleOutput = false;
+    }
+
     public function test(): void
     {
         $this->assertEquals('content', \trim((new Artisan())::output()));
     }
 }
PACKAGE:  laravel/framework >=5.7

AddParentBootToModelClassMethodRector

Add parent::boot(); call to boot() class method in child of Illuminate\Database\Eloquent\Model

 use Illuminate\Database\Eloquent\Model;

 class Product extends Model
 {
     public function boot()
     {
+        parent::boot();
     }
 }
PACKAGE:  laravel/framework >=5.7