New features in laravel 9 Updated 2022

    By: Thad Mertz
    2 years ago

    In this guide we are going to see new features introduced in Laravel 9. Laravel is a php Framework and every year now we see latest improvements in Laravel. Industry recognise laravel as a fast and reliable php framework. Let's see what new feature we have now in latest version of Laravel 9.


    Route Grouping

    Now we can group our routes by Controller here is how

    // Old way to define routes
    
    
    Route::get('/',  [PostsController::class, 'index'])->name('home');
    
    Route::post('posts',  [PostsController::class, 'store']);
    
    Route::get('posts/{post}',  [PostsController::class, 'show']);
    

    New way with Controller based grouping

    // All theses routes fall under PostsController so they are grouped
    
    Route::controller(PostsController::class)->group(
        function(){
    
            Route::get('/',   'index')->name('home');
    
            Route::post('posts',   'store');
    
            Route::get('posts/{post}',   'show');
    
            Route::get('/add', 'add');
        }
    );
    


    Helper Functions


    We have new helper functions such as str() and to_route

    // str() is similar to Str::of
    
    // So we can use it like this
    
    str($value)->upper()
    
    // or
    
    str($value)->lower()
    

    For redirect we use to do this

    Route::get('/about', function () {
        return redirect()->route('home');
    });
    

    Now we can redirect using helper function

    Route::get('/about', function () {
        return to_route('home');
    });
    


    Accessor and mutator in laravel 9


    So old way of defining accessor and mutator is

       // Accessor 
    
       public function getTitleAttribute($value)
       {
           return str($value)->upper();
       }
    
    
       // Mutator
    
       public function setTitleAttribute($value)
       {
           $this->attributes['title'] = str($value)->lower();
       }
    

    Now we can do this in single function

        /**
         * @return Attribute
         */
        public function title(): Attribute
        {
            return new Attribute(
                 get: fn($value) =>  str($value)->upper(),
                 set: fn($value) =>  str($value)->lower(),
            );
        }
    

    But we need to include this class

    use Illuminate\Database\Eloquent\Casts\Attribute;
    


    Enums For Attribute casting and Route Binding


    We can do attribute casting so lets say in our model we have

        protected $casts = [
            'status' => PostStatus::class
        ];
    

    Here we are storing if the post is public or private.

    Now we can make sure the value provided is according to our Enum file here is the enum file

    <?php
    
    
    namespace App\Enums;
    
    
    enum PostStatus: string
    {
        case Onhold = 'onhold';
        case Published = 'published';
        case Archived = 'archived';
    }
    

    So when we save a post

            $post = new Post();
            $post->title = ' Some test title';
            $post->status = PostStatus::Published;  // Enum is checking if allowed enums are passed as data
            $post->content = 'test content';
            $post->save();
    
           // this will give error.
    
            $post->status = 'something';
    
          // This will work of as published is present in our Enum file
            $post->status = PostStatus::Published; 
    


    Enums In Route Binding


    We can also use Enum in route binding

    Route::get('/posts/{status}', function(PostStatus $status){
        dd($status);
    });
    

    So Here is how it will work

    // This will give error.
    
    http://localhost/posts/something
    
    // This will work fine.
    
    http://localhost/posts/published
    
    

    Check our video guide for better understanding