Good content takes time and effort to come up with.

Please consider supporting us by just disabling your AD BLOCKER and reloading this page again.







New Features In Laravel 8 | StackCoder


New Features In Laravel 8


07th September 2020 4 mins read
Share On     Share On WhatsApp     Share On LinkedIn


Hello fellas! Today is a big day for the Laravel community as Laravel 8 has released the early access to the documentation. Though the features are still on its way to update, there are tons of loaded features to give it a try.


Server Requirements


Make sure you have the following Laravel 8 requirements installed


  1. PHP >= 7.3
  2. BCMath PHP Extension
  3. Ctype PHP Extension
  4. Fileinfo PHP extension
  5. JSON PHP Extension
  6. Mbstring PHP Extension
  7. OpenSSL PHP Extension
  8. PDO PHP Extension
  9. Tokenizer PHP Extension
  10. XML PHP Extension

Larave 8 Installation


composer create-project --prefer-dist laravel/laravel blog

PHP Artisan Serve Autoreload If Any Changes


Old Versions

In earlier versions of Laravel if you made any changes to .env files then you had to manually stop and start the server.


New Version

In the newer versions of Laravel as soon as you make any changes to the .env file, it's automatically detected and the server is reloaded by Laravel.


Routes Changes (web.php)


Old Versions

<?php

use Illuminate\Support\Facades\Route;

Route::get('/', 'TestController@index');


New Version

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TestController;

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



Models Directory


Old Versions

In earlier versions of Laravel the models used to be under the App namespace. I personally used to create an app/Models directory and would put all my models in that folder.


New Version

In the newer version of Laravel, we have an app/Models directory be default.


Model Factory Classes


Old Versions

We just had a normal plain PHP files under database/factories directory


New Version

Eloquent model factories have been entirely re-written as class-based factories.


database / factories / UserFactory.php

<?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),
        ];
    }
}


Usage

use App\Models\User;

User::factory()->count(50)->create();

Model Factory States


Similar to the older factory states but now inside the factory classes.


/**
 * Indicate that the user is suspended.
 *
 * @return \Illuminate\Database\Eloquent\Factories\Factory
 */
public function suspended()
{
    return $this->state([
        'account_status' => 'suspended',
    ]);
}

Migrations Update (startingValue)


Soon we will be having startingValue to it as a starting id value in our database.


Eg: When you create new orders for customers and they don't want to know they were the first ones. LOL


public function up() {
  Schema::create('Orders', function(Blueprint $table){
    $table->id()->startingValue('1000');
    ...

   


Migration Squashing


When you have a large Laravel application you migrations might grow over the time and start flooding like anything.


Now you can squash many migrations files into 1 migration file, which will then get saved inside the database/schema directory.


php artisan schema:dump

// Dump the current database schema and prune all existing migrations...
php artisan schema:dump --prune


After squashing when you run the migration then first the database/schema migrations will run, then database/migrations directory migrations.


Job Batching


After performing a batch of jobs you can perform some other stuff.


The new batch method of the Bus facade may be used to dispatch a batch of jobs. Of course, batching is primarily useful when combined with completion callbacks. So, you may use the thencatch, and finally methods to define completion callbacks for the batch. Each of these callbacks will receive an Illuminate\Bus\Batch instance when they are invoked:

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)),
    new ProcessPodcast(Podcast::find(3)),
    new ProcessPodcast(Podcast::find(4)),
    new ProcessPodcast(Podcast::find(5)),
])->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 Rate Limiting


Rate limiters are defined using the RateLimiter facades for method. The for method accepts a rate limiter name and a Closure that returns the limit configuration that should apply to routes that are assigned this rate limiter:


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

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


Since rate limiter callbacks receive the incoming HTTP request instance, you may build the appropriate rate limit dynamically based on the incoming request or authenticated user:

RateLimiter::for('uploads', function (Request $request) {
    return $request->user()->vipCustomer()
                ? Limit::none()
                : Limit::perMinute(100);
});


Tailwind Pagination Views


The Laravel paginator has been updated to use the Tailwind CSS framework by default.


Laravel Jetstream


Laravel Jetstream is a beautifully designed application scaffolding for Laravel. Jetstream provides the perfect starting point for your next project and includes login, registration, email verification, two-factor authentication, session management, API support via Laravel Sanctum, and optional team management.


Jetstream is designed using Tailwind CSS and offers your choice of Livewire or Inertia scaffolding.


Conclusion


More coming on soon. Stay tuned...


I hope this article helped you. Please share it with your friends.


Factory States For Clean And Fluent Laravel Testing

Factories To Speed Up Test-Driven Development In Laravel

Stripe Payment Integration With Laravel

Writing Clean Code In PHP

Foreign Key Migrations Simplified In Laravel 7.x

Global Data In All Laravel Blade Pages

Select2 Dropdown

Observers In Laravel

Relationship Table Data With Route Model Binding In Laravel

Getting Started With AMP (Accelerated Mobile Pages)

Setup AMP (Accelerated Mobile Pages) In PHP Laravel

Add Analytics To AMP (Accelerated Mobile Pages) HTML Pages

Add Google ADS In AMP (Accelerated Mobile Pages) Website







Author Image
AUTHOR

Channaveer Hakari

I am a full-stack developer working at WifiDabba India Pvt Ltd. I started this blog so that I can share my knowledge and enhance my skills with constant learning.

Never stop learning. If you stop learning, you stop growing