laravel guidence

If you’re currently using Laravel 9.x for your website or bespoke web app, it’s crucial to stay ahead of the curve and upgrade to Laravel 10.x, which was released on February 14, 2023. This ensures that your system remains secure and benefits from the latest features. In this guide, we’ll walk you through the essential steps to seamlessly upgrade from Laravel 9.x to Laravel 10.x while preserving the

Key Points for Upgrade

1. Updates to Dependencies

A. PHP 8.1 + Required

Ensure that your system runs PHP 8.1.0 or later, as Laravel 10.x mandates this version. Check your PHP version using the command:

./vendor/bin/sail php -v

 

B. Needs Composer 2.2.0.

Update dependencies in your composer.json file:

“laravel/framework”: “^10.0”, “laravel/sanctum”: “^3.2”, “doctrine/dbal”: “^3.0”, “spatie/laravel-ignition”: “^2.0”, “laravel/passport”: “^11.0”

 

If using PHPUnit 10, remove processUncoveredFiles from your phpunit.xml configuration:

“nunomaduro/collision”: “^7.0”, “phpunit/phpunit”: “^10.0”

 

Update any third-party packages to match Laravel 10.x requirements.

2. Stability at a Minimum

Ensure that the minimum-stability in your composer.json is set to “stable” or remove the setting if it’s set by default.

“minimum-stability”: “stable”

3. Update a Web Application

Modify the code invoking usePublicPath for a customized ‘public path’:

app()->usePublicPath(__DIR__.’/public’);

4. Cache Refresh

Use Laravel’s new cache:prune-stale-tags function to handle Redis cache tag support efficiently. Add this Artisan command to your App\Console\Kernel class:

$schedule->command(‘cache:prune-stale-tags’)->hourly();

5. Database Expression and Database Update

Rewrite database expressions using DB::raw. Update manually casted database expressions to invoke the getValue method.

use Illuminate\Support\Facades\DB; $expression = DB::raw(‘select 1’); $string = $expression->getValue(DB::connection()->getQueryGrammar());

6. Constructor of Query Exceptions

Adjust code if your application manually throws

Illuminate\Database\QueryException.

7. Columns ULID

Specify column name when invoking the ULID method during migration:

$table->ulid(‘ulid’);

8 . Update Eloquent

A. ‘Dates’ Model Property

Replace the deprecated $dates field with the $casts property:

protected $casts = [ ‘deployed_at’ => ‘datetime’, ];

 

B. Method of Relationship

Remove the getBaseQuery method on the

Illuminate\Database\Eloquent\Relations\Relation class.

9. Logging Update

A. Monolog 3

Upgrade Monolog to version 3.x if your application interacts directly with Monolog.

10. Update Queues

Replace obsolete approaches Bus::dispatchNow and dispatch_now with Bus::dispatchSync and dispatch_sync.

11. Update Routing

A. Middleware Aliases

Rename $routeMiddleware to $middlewareAliases in App\Http\Kernel.

 

B. Return Values of Rate Limiters

Handle returned values when executing RateLimiter::attempt method.

 

C. The Redirect::home Technique

Replace deprecated Redirect::home with explicit route redirection:

return Redirect::route(‘home’);

12. Update Testing

Replace deprecated MocksApplicationServices trait with Event::fake, Bus::fake, and Notification::fake.

13. Update Validation

Adjust closure-based custom validation rules using the $fail callback.

Additional Information

Check the Laravel GitHub repository for more changes and updates. Some updates are optional but recommended for a smooth transition.

Wrapping Up

Thoroughly read the Laravel 10.x documentation before upgrading to minimize the risk of technical issues. For expert assistance in Laravel development, Tec Sense offers comprehensive services, from customization to maintenance. Contact us for a free quote at sales@tec-sense.com.

  • admin Ankit Patel
  • calendar 9 December, 2023