Overview
Laravel 8 is now released with new features like Laravel Jetstream, Models directory, Improving Rate Limiting, Queue Closure Dispatch, Improved Maintenance Mode, Job Batching,Migration Squashing and many more features.
Laravel Jetstream
Laravel 8 provides laravel jetstream in this login,registration, email verification, two-factor authentication, session management and many more things.
Laravel Jetstream replaces the legacy Laravel authentication UI available for previous Laravel versions. Laravel Jetstream is a beautifully designed application for laravel.
Jetstream is designed using Tailwind CSS and also provides choice Livewire or Inertia.
Model Directory
In Laravel 7 or lesser version app/Models directory doesn’t exist but now laravel 8 provides app/Models directory. When we can execute this command
php artisan make:model Product
this model create in app/Models/Product.php
Improving Rate Limiting
In Laravel 7 or lesser version for access rate limit using throttle middleware.
Laravel 8 improving rate limiting from 60 requests per minute to 1000 requests per minute
Rate limiters are defined using the RateLimiter facade’s for method also It even supports different limits based upon a model so we can limit the amount of requests based upon a dynamic parameter. Using Rate limiting we can reduce the amount of traffic for a given route.
use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Support\Facades\RateLimiter; RateLimiter::for('global', function (Request $request) { return Limit::perMinute(1000); });
we can also assign throttle middleware in laravel 8 like this
Route::middleware(['throttle:uploads'])->group(function () { Route::post('/audio', function () { // }); Route::post('/video', function () { // }); });
Laravel provides us for accessing given route on given times per minute on per IP address so we can use rate limit on that way
RateLimiter::for('uploads', function (Request $request) { return $request->user()->vipCustomer() ? Limit::none() : Limit::perMinute(100)->by($request->ip()); });
Dynamic Blade Components
Laravel 8 provides Dynamic Blade Components for render components for run time.
using that way we can render our component at runtime.
Example: We have three types of components for showing messages that are success, error and warning.
In User Controller we can write this code
return view('user.edit', ['messageComponent' => 'success']);
In Success.blade.php file we can write this code
<x-dynamic-component :component="$messageComponent" class="mt-4" />
The basic meaning of dynamic blade components is we can render a component based on the value of a variable.
Artisan serve Improvements
Laravel 8 improves php artisan serve command . In Serve improvement when the environment variable changed in the .env file in local then the serve command stopped and restarted again.
Routing Namespace Updates
Laravel 7 default provides route prefixing using $namespace property in RouteServiceProvider on the bases of controller route definitions but in Laravel 8 there is no prefixing in route namespace. In this $namespace property is null.
In router files define your route that way
use App\Http\Controllers\UserController; Route::get('/users', [UserController::class, 'index']);
Calls to action related methods use that way
action([UserController::class, 'index']); return Redirect::action([UserController::class, 'index']);
Migration Squashing
In this feature we can execute this command in our terminal
php artisan schema:dump
This command generate a schema file in database/schema folder in our project directory
which is basically the structure of your whole database.
If we want to dump your current database in this schema file so use this command
php artisan schema:dump --prune
After this command we fire migrate command no migration created in our database ,firstly execute our schema SQL file first then execute those migrations that are not part of schema dump.
Job Batching
Job Batching is another new feature in laravel 8. This feature allows dispatch a batch of jobs to be completed executing and also perform action with a batch of jobs.
use App\Jobs\ProcessPodcast; use App\Podcast; use Illuminate\Bus\Batch; use Illuminate\Support\Facades\Batch; use Throwable; $batch = Bus::batch([ new ProcessPodcast(Podcast::find(1)), new ProcessPodcast(Podcast::find(2)), ])->then(function (Batch $batch) { // All jobs completed successfully... })->catch(function (Batch $batch, Throwable $e) { // First batch job failure detected... })->finally(function (Batch $batch) { // The batch has finished executing... })->dispatch(); return $batch->id;
Improved Maintenance Mode
Laravel 7 or lesser using php artisan down command live application/website is down for sometime but laravel 8 improves this functionality.
In laravel 8 we can allow a list of Ip addresses for accessing applications/websites when the website is in Maintenance mode using a secret token.
like this:
php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"
In previous Laravel versions when the application/website is in maintenance mode than the user facing some issues like application/website is not working for resolving this issue laravel 8 provides pre-render maintenance mode view.
Queue Closure Dispatch/Chain
In laravel 7 we don’t have any catch method for handling the fail queue closure. Laravel 8 provides a catch method for handling failed queue closure using catch method.
that way we can handle:
use Throwable; dispatch(function () use ($podcast) { $podcast->publish(); })->catch(function (Throwable $e) { // This job has failed... });
Time Testing Helpers
Laravel 8 provides us for testing. We can execute a test class with helpers to modify in current time.
public function testModifyTime(){ //Travel into future $this->travel(5)->minutes(); $this->travel(5)->hours(); $this->travel(5)->days(); // Travel into the past $this->travel(-5)->hours(); //Travel back to present time $this->travelBack(); }
Model Factory Classes
Laravel 7 provides a Model factory for creating dummy data and stored in our database.
$user = factory(App\User::class)->create();
If we want to add relation in existing model factory than we can create another factory like this
$users = factory(App\User::class, 3) ->create() ->each(function ($user) { $user->posts()->save(factory(App\Post::class)->make()); });
Now in laravel 8 add relation in existing model no need to create another model factory just need to use relationship and store data in database.
$users = User::factory() ->hasPosts(3, [ 'published' => false, ]) ->create();
Conclusion
So are you guys ready to adopt laravel 8, I’m sure you are. These new features are very exciting and helpful for us. Thank you so much for reading this blog. Hope this blog is helpful to all.