Laravel 9 brings modern PHP features and developer experience improvements. Symfony 6 components, PHP 8 requirements, and anonymous migrations enhance development. At ZIRA Software, we prepare projects for Laravel 9 migration.
PHP 8 Requirement
Minimum PHP 8.0:
- Native attributes
- Named arguments
- Constructor property promotion
- Union types
- Match expressions
// Constructor property promotion
class User {
public function __construct(
public string $name,
public string $email,
public ?string $phone = null,
) {}
}
Symfony 6 Components
Updated dependencies:
- symfony/console 6.0
- symfony/http-foundation 6.0
- symfony/http-kernel 6.0
- symfony/routing 6.0
Anonymous Migrations
// Before: Named migration class
class CreateUsersTable extends Migration
{
public function up() { }
}
// Laravel 9: Anonymous class
return new class extends Migration
{
public function up()
{
Schema::create('users', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->timestamps();
});
}
};
Benefits:
- No class name conflicts
- Cleaner migration files
- Easier to manage
Improved Route Binding
// Implicit enum binding
enum Category: string {
case Tech = 'tech';
case News = 'news';
}
Route::get('/posts/{category}', function (Category $category) {
return Post::where('category', $category->value)->get();
});
Full-Text Indexing
Schema::create('posts', function (Blueprint $table) {
$table->id();
$table->string('title');
$table->text('content');
$table->fullText(['title', 'content']);
});
// Query
Post::whereFullText(['title', 'content'], 'Laravel')->get();
Controller Route Groups
Route::controller(PostController::class)->group(function () {
Route::get('/posts', 'index');
Route::post('/posts', 'store');
Route::get('/posts/{post}', 'show');
});
Improved Accessors/Mutators
use Illuminate\Database\Eloquent\Casts\Attribute;
class User extends Model
{
// New syntax
protected function name(): Attribute
{
return Attribute::make(
get: fn ($value) => ucfirst($value),
set: fn ($value) => strtolower($value),
);
}
}
Conclusion
Laravel 9 modernizes the framework with PHP 8 features and improved developer experience. Prepare for migration by ensuring PHP 8 compatibility.
Planning Laravel 9 migration? Contact ZIRA Software for upgrade assistance.