Introduction
It’s been a while when we last discussed What’s new in Laravel 6. And here we are today discussing about what’s new in Laravel 8.x. Whether you are upgrading to Laravel 8 from Laravel 7 or just getting started from Laravel 8 this blog is going to get very helpful.
We are going to discuss the below mentioned points in this blog.
- Laravel Jetstream
- Models Directory
- Model Factory Classes
- Migration Squashing
- Improved Rate Limiting
- Time Testing Helpers
- Dynamic Blade Helpers
Laravel JetStream
If you are following Laravel from Previous versions you might be knowing about Laravel UI Scaffolding. Laravel Jetstream is an upgrade on the previously present Laravel UI Scaffolding. The main task of it is to provide a starting point for new projects which includes:
- Login and Registration
- Email Verification
- Two-factor Authentication
- Session Management
- Laravel Sanctum API Support
- Tailwind CSS
- Choices of two frontend slacks i.e Livewire + Blade or Interia.JS + Vue.JS.
How to use it ?
In order to use Laravel JetStream, you can simply use Laravel Installer to install it.
1 |
laravel new bagisto --jet |
Models Directory
The old Models Directory is back again. Remember using a separate Models folder for creating Models before Laravel 5. The Models were shifted to app/ directory. Now with Laravel 8.x it shifted back to app/Models.
Model Factory Classes
Eloquent Model Factory are now classes based. It leads to an improvement in support for relationships between factories. (example: multiple posts of a user.).
Let’s check out how is it different from Laravel 7.x
User factory In Laravel 7.x
1 2 3 4 5 6 7 8 9 10 11 12 |
use Faker\Generator as Faker; use Illuminate\Support\Str; $factory->define(App\User::class, function (Faker $faker) { return [ 'name' => $faker->name, 'email' => $faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password 'remember_token' => Str::random(10), ]; }); |
User factory In Laravel 8.x
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
namespace Database\Factories; use App\Models\User; use Illuminate\Database\Eloquent\Factories\Factory; use Illuminate\Support\Str; class UserFactory extends Factory { /** * The name of the factory's corresponding model. * * @var string */ protected $model = User::class; /** * Define the model's default state. * * @return array */ public function definition() { return [ 'name' => $this->faker->name, 'email' => $this->faker->unique()->safeEmail, 'email_verified_at' => now(), 'password' => '$2y$10$92I.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', 'remember_token' => Str::random(10), ]; } } |
You can see the model property and definition method. The definition method then returns the model attributes.
Migration Squashing
If your application contains many migration files, you can now squash them into a single SQL file. This file will be executed first when running migrations, followed by any remaining migration files that are not part of the squashed schema file. Squashing existing migrations can decrease migration file bloat and possibly improve performance while running tests.
Improved Rate Limiting
Laravel 8 brings improvements to existing rate limiting functionality while supporting backward compatibility with the existing throttle middleware and offering far more flexibility. Laravel 8 has the concept of Rate Limiters that you can define via a facade:
1 2 3 4 5 6 |
use Illuminate\Cache\RateLimiting\Limit; use Illuminate\Support\Facades\RateLimiter; RateLimiter::for('global', function (Request $request) { return Limit::perMinute(1000); }); |
The for() method takes the HTTP request instance, giving you full control over limiting requests dynamically.
Time testing helpers
Laravel users have enjoyed full control over time modification via the excellent Carbon PHP library. Laravel 8 brings this one step further by providing convenient test helpers for manipulating the time within tests:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
$this->travel(5)->milliseconds(); $this->travel(5)->seconds(); $this->travel(5)->minutes(); $this->travel(5)->hours(); $this->travel(5)->days(); $this->travel(5)->weeks(); $this->travel(5)->years(); // Travel into the past... $this->travel(-5)->hours(); // Travel to an exact time... $this->travelTo(now()->subHours(6)); // Return back to the present time... $this->travelBack(); |
Dynamic Blade Component
Sometimes you need to render a blade component dynamically at runtime. Laravel 8 provides the <x-dynamic-component /> to render the component:
1 |
<x-dynamic-component :component="$componentName" class="mt-4" /> |
So these all features are introduced with the latest version of Laravel 8.x.
For more information regardin Laravel 8, go to: laravel’s website
Sources:
Laravel’s Website
Laravel News