Difference between Laravel 7 and 8
Blog Standard

Difference between Laravel 7 and 8

Laravel is web application framework helps for building modern and full stack web applications.It has powerful features like thorough dependency injection, an expressive database abstraction layer, queues and scheduled jobs, unit and integration testing, and more.

Laravel 7 was released on March 3rd, 2020 continues the improvements made in Laravel 6.x by introducing Laravel Sanctum, routing speed improvements, custom Eloquent casts, Blade component tags, fluent string operations, a developer focused HTTP client, first-party CORS support, improved scoping for route model binding, stub customization, database queue improvements, multiple mail drivers, query-time casts, a new artisan test command, and a variety of other bug fixes and usability improvements.

Laravel 8 released on September 8th, 2020 the improvements made in Laravel 7.x by introducing Laravel Jetstream, model factory classes, migration squashing, job batching, improved rate limiting, queue improvements, dynamic Blade components, Tailwind pagination views, time testing helpers, improvements to artisan serve, event listener improvements, and a variety of other bug fixes and usability improvements.

app/Models Directory

Laravel 7 has no Models directory, and it become incovinient when number of models used in projects exceeds, especially in larger projects. While the artisan:make model command will create the model in the app/Models directory in Laravel 8.

New Landing Page

Laravel 8 comes with a new landing page for a brand new install compared to Laravel 7.

Controllers Routing Namespacing

In Laravel 7, the RouteServiceProvider had an attribute called namespace that was used to prefix the controllers in the routes files. That created a problem when trying to use a callable syntax on controllers, causing Laravel to mistakenly double prefix it.

In Laravel 7 the $namespace property was set to App\Http\Controllers. This change means that controller route definitions will no longer include automatic namespace prefixing.

// routes/web.php

Route::get('/posts', '[email protected]');


In Laravel 8 this attribute was removed and now import and use it without the issue. It can also be used for single action controllers that have the __invoke method.


// routes/web.php
use App\Http\Controllers\AdminController;

Route::get('/posts', [AdminController::class, 'index']);

Maintenance Mode

In Laravel 7 , have to specify the IPs that would still be allowed to access the application with the allow option:

php artisan down --allow=127.0.0.1 --allow=192.168.0.0/16

In Laravel 8,no longer need to allow certain IPs explicitly. Instead, you can use the secret option to create a maintenance mode bypass token:

php artisan down --secret="1630542a-246b-4b66-afa1-dd72a4c43515"

You can then access your application while in maintenance mode by appending the token to your application's URL, e.g., https://webrins.com/163087842a-246b-4b66-afa1-dd72a4c43515. The bypass cookie will be saved to your browser, and you can continue navigating your application from its normal URL, e.g., https://webrins.com.

Better Rate Limiting

Laravel includes a middleware to rate limit access to routes within your application. To get started, assign the throttle middleware to a route or a group of routes. The throttle middleware accepts two parameters that determine the maximum number of requests that can be made in a given number of minutes.

Rate limiting in Laravel 7

Route::middleware('auth:api', 'throttle:10,1')->group(function () {
  Route::get('/login', function () {
    //
  });
});

Rate limiting in Laravel 8

In Laravel 8, can define rate limiters in app/Providers/RouteServiceProvider.php using the for method of the RateLimiter facade. The for method will accept a name and a Closure, which returns the rate limiting details that set up.

use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;

RateLimiter::for('login', function (Request $request) {
  return Limit::perMinute(10);
});

You can now apply this pre-configured rate limiter to routes with throttle: followed by the rate limiter name.

Route::middleware(['throttle:login'])->group(function () {
  Route::post('/login', function () {
    //
  });
  Route::post('/register', function () {
    //
  });
});

Blade component attributes

If you extended a Blade component (e.g. having a component called DangerButton, that extended another component called Button) in Laravel 7, the child button wouldn't have had the $attributes passed down to it. That has changed in Laravel 8 - all child components will the $attributes available, making it easier to build extended components.

Cleaner syntax for closure based event listeners

In Laravel 7 when registering a closure based event listener, you had to first define the event class, then register the closure, and probably type hint the event class for the closure e.g.:

Event::listen(OrderSoftware::class, function(OrderSoftware $event) { 
    // Do something
});

In Laravel 8, you'll be able to skip the first definition of the event class as the framework will be able to infer it from the type hinted argument - e.g.:

Event::listen(function(OrderSoftware $event) { 
    // Do something
});

Laravel JetStream & Laravel Fortify

Laravel 7 had a package called laravel/ui whcih provided basic authentication scaffolding - giving you the front (Vue & Bootstrap 4) & back end code for a fully working login system for your application.

In Laravel 8, Laravel JetStream - a new open source package for Laravel 8 to give you an even bigger head start with your applications.

It's a scaffolding package that will set you up with complete front & back end code for lots of common functionality in web applications, including:

  • Authentication (login, access a dashboard page, logout, password reset etc)
  • Two factor authentication (via an app such as Google Authenticator) with no setup, including features such as recovery codes
  • Browser session management (let a user see where else they are logged in, and force a logout of all other sessions)
  • API token management for users powered by Laravel Sanctum, with full customisable permissions (similar to oAuth scopes)
  • Full "teams" functionality (users can create teams, invite others users to teams, see other team members, manage roles (& permissions) of team members, switch between multiple teams etc)
  • User profile editing (let's users update their details) including profile photos (easily toggable on / off in config) and password changing
  • Delete account functionality
  • JavaScript powered front end (see below) for nice smooth interactions without full page reloads, giving a really slick feel

Model factory classes

User factory in Laravel 7.x:

// database/factories/UserFactory.php

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:

// database/factories/UserFactory.php

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$92IXUNpkjO0rOQ5byMi.Ye4oKoEa3Ro9llC/.og/at2.uheWG/igi', // password
            'remember_token' => Str::random(10),
        ];
    }
}

In Laravel 8, factories are now classes that extend the base Laravel factory class. Glancing at the default file, you'll see the model property and definition method. The definition method then returns the model attributes.
Compare this to Laravel 7 and below, where the UserFactory is defined with a Closure and returns the specified model attributes.Both of these still have access to Faker, as expected. Let's look at the difference between using factories in Laravel 7 versus Laravel 8.

Using factories in Laravel 7:

Before this update, you would use the factory helper function in your seeder files to generate model instances.

// database/seeds/UserSeeder.php

class UserSeeder extends Seeder
{
  /**
  * Run the database seeds.
  *
  * @return void
  */
  public function run()
  {
    // Create three App\User instances
    $users = factory(App\User::class, 3)->create();
  }
}

Using factories in Laravel 8:

With Laravel 8, you can use the factory directly on the model instance. This is because the default models now include a HasFactory trait, as shown in the simplified code below.

// database/seeders/UserSeeder.php

class UserSeeder extends Seeder
{
  /**
    * Run the database seeds.
    *
    * @return void
    */
  public function run()
  {
    // Create three App\User instances
    User::factory(3)->create();
  }
}
// app/Models/User.php

Migration Squashing

In Laravel 7, have to scroll for five minutes when you open up your migrations folder

But using migration squashing feature in Laravel 8 , you can now condense your migration files into a single SQL file with the following commands:


php artisan schema:dump
php artisan schema:dump --prune

In Laravel 8, Laravel will write the new schema file to database/schema. Then when you run your migrations, Laravel will run the SQL from the schema file first before moving on to anything created later in the migrations folder.

I hope you understand all the differences laravel 7 and laravel 8.For more details go tohttps://laravel.com/