Introduction to Laravel 9
Blog Standard

Introduction to Laravel 9

Laravel is a MVC php Framework created by  Taylor Otwell.Laravel is a powerful toolkit to create full featured web applications.Laravel basically an open source software with rich functionalities which helps us create our website effectively. 

Laravel 9 is the new version of laravel framework released  on 8th february 2022.Laravel 9 adds many useful  new features. 

  • Requirements of Laravel 9

The Installation of laravel needs some requirements, these requirements are essential for laravel installation and smooth running. 

              1.Minimum Php Requirement

                 Laravel requires at least php version 8 because laravel 9 uses symfony 6 which requires at least php8.The lower versions need PHP >= 7.2.5.

               2.Composer

                Laravel utilizes Composer to manage its dependencies. So, before using Laravel, make sure you have Composer installed on your machine.

               3.Node Js

               NodeJS and Laravel are both server side technologies, using the two in conjunction doesn't really make much sense without a little understanding and make your                        development much easier.

  • Symfony Mailer

In previous versions laravel uses swift mailer package for mail applications. The new version of laravel replaces swift mailer by symfony mailer. Symfony Mailer uses the symfony/mime package. This package provides a few handy objects for creating messages that follow the MIME standard. One of these classes is Email, which provides a high-level API to quickly create an email message. 

  • Controller Route groups

Laravel 9 Introduces a new feature Controller Route Groups. This feature groups all routes of a controller in a single function.

Route::Controller(postController::class)->group( function() {

                         Route::get('/post'.'show');

                         Route::post('/posts'.'store');

                 }

  • Flysystem 3.X

Flysystem is a file storage library for PHP. It provides one interface to interact with many different types of filesystems.Old versions of laravel uses Flysystem 2.X . Laravel 9 upgrades it to flysystem 3.X.

  • Eloquent Accessors and Mutators

Laravel Accessors and Mutators are custom, user defined methods. Accessors are used to format the attributes when you retrieve them from the database. Mutators are used to format the attributes before saving them into the database.The Previous releases of Laravel, the only way to define accessors and mutators was by defining prefixed methods are shown below,

public function getNameAttribute($value)

{

return strtoupper($value);

}

 public function setNameAttribute($value)

{

$this->attributes['name'] = $value;

}

 

Laravel 9.x offers a new way to define Eloquent accessors and mutators.

 

use Illuminate\Database\Eloquent\Casts\Attribute;

 public function name(): Attribute

{

return new Attribute(

get: fn ($value) => strtoupper($value),

set: fn ($value) => $value,

);

}

  • Enum Eloquent attribute casting

Laravel Enum is a package that adds support for creating enums in PHP and includes a generator for Laravel.Eloquent now allows you to cast your attribute values to backed enums.

 

use App\Enums\ServerStatus;

/**

* The attributes that should be cast.

*

* @var array

*/

protected $casts = [

'status' => ServerStatus::class,

];

  • Route Bindings with enums

The new version provides the support for enums because php 8.1 supports enums.Laravel 9.x introduces the ability to type-hint an Enum on your route definition and Laravel will only invoke the route if that route segment is a valid Enum value in the URI. Otherwise, an HTTP 404 response will be returned automatically. 

enum Category: string

{

case Fruits = 'fruits';

case People = 'people';

}

Route::get('/categories/{category}', function (Category $category) {

return $category->value;

});

  • FullText() 

If you are using MySql database for your application, You can now use the fulltext method to generate full text indexes.

 $table->text('content')->fullText();

                      or

                $data=DB::table('post')

                    ->whereFullText('content','laravel')

                   ->get();

  • Scout Database Engine

This package provides a generic Laravel Scout driver which performs full-text search on indexed model data using an SQL database as storage backend. Indexed data is stored in normalized form, allowing efficient and fuzzy search which does not require a full and/or exact match.

  • Rendering Inline Blade Templates

Sometimes you may need to transform a raw Blade template string into valid HTML. You may accomplish this using the render method provided by the Blade facade. The render method accepts the Blade template string and an optional array of data to provide to the template:

 

use Illuminate\Support\Facades\Blade;

return Blade::render('Hello, {{ $name }}', ['name' => 'Julian Bashir']);

  • Slot Name Shortcuts

In previous versions,

slot names were provided using a name attribute on the x-slot tag:

<x-alert>

<x-slot name="title">

Server Error

</x-slot>

<strong>Whoops!</strong> Something went wrong!

</x-alert>

but now we can simply use,

<x-slot:title>

Server Error

</x-slot>

  • Checked and selected directives

In laravel @checked directive can be used to indicate if a checkbox is checked or not if it is true returns checked.

name="active"

value="active"

@checked(old('active', $user->active)) />

similarly in select tag we can use @select directive,

@foreach ($product->versions as $version){{ $version }} @endforeach <

  • Bootstrap 5 pagination views

The latest version includes pagination views built using Bootstrap 5. You may call the paginator's useBootstrapFive method within the boot method of your App\Providers\AppServiceProvider class:

 use Illuminate\Pagination\Paginator;

 /**

* Bootstrap any application services.

*

* @return void

*/

public function boot()

{

Paginator::useBootstrapFive();

}

  • Validation for Nested array data

Laravel 9 provides an improved method for validating nested array data using the Rule::forEach method.

use App\Rules\HasPermission;

use Illuminate\Support\Facades\Validator;

use Illuminate\Validation\Rule;

$validator = Validator::make($request->all(), [

'companies.*.id' => Rule::forEach(function ($value, $attribute) {

return [

Rule::exists(Company::class, 'id'),

new HasPermission('manage-company', $value),

];

}),

]);

Installation of Laravel 9

1.Installation in windows

Laravel 9 can be Install via composer,open command prompt, change the directory to xampp/htdocs then type the following command,

        composer create-project --prefer-dist laravel/laravel:^7.0 blog   

For run your first project, 

       cd blog

       php artisan serve

 

2.Installation in macOS 

If you're developing on a macOS first, install Docker Desktop in your pc.You can create  a new Laravel project on macOS with  a simple terminal command.

curl -s "https://laravel.build/example-app" | bash

cd example-app

./vendor/bin/sail up

 example-app is the name of your project.

 

3.Installation in Linux

Installation of laravel in linux is the same as macOS.

curl -s "https://laravel.build/example-app" | bash

cd example-app

./vendor/bin/sail up

 

4.Installation via sail Services

sail services in laravel is a light-weight command-line interface for interacting with Laravel's default Docker development environment. Sail provides a great starting point for building a Laravel application using PHP, MySQL, and Redis without requiring prior Docker experience.

When creating a new Laravel application via Sail, you may use the with query string variable to choose which services should be configured in your new application's docker-compose.yml file. Available services include mysqlpgsqlmariadbredismemcachedmeilisearchminioselenium, and mailhog:

curl -s "https://laravel.build/example-app?with=mysql,redis" | bash