Laravel 8 modernizes development patterns. Class-based model factories improve testing, job batching enables complex workflows, and enhanced rate limiting protects APIs.
New Model Factories
Before Laravel 8:
$factory->define(User::class, function (Faker $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
];
});
Laravel 8:
php artisan make:factory UserFactory --model=User
// database/factories/UserFactory.php
namespace Database\Factories;
use Illuminate\Database\Eloquent\Factories\Factory;
class UserFactory extends Factory
{
public function definition()
{
return [
'name' => $this->faker->name(),
'email' => $this->faker->unique()->safeEmail(),
];
}
public function admin()
{
return $this->state(function (array $attributes) {
return ['role' => 'admin'];
});
}
}
Usage:
User::factory()->count(10)->create();
User::factory()->admin()->create();
Job Batching
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
$batch = Bus::batch([
new ProcessOrder($order1),
new ProcessOrder($order2),
new ProcessOrder($order3),
])->then(function (Batch $batch) {
// All jobs completed successfully
})->catch(function (Batch $batch, Throwable $e) {
// First batch job failure detected
})->finally(function (Batch $batch) {
// Batch has finished executing
})->dispatch();
return $batch->id;
Rate Limiting
use Illuminate\Cache\RateLimiting\Limit;
use Illuminate\Support\Facades\RateLimiter;
RateLimiter::for('api', function (Request $request) {
return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip());
});
// Per-route limits
Route::middleware('throttle:api')->group(function () {
Route::apiResource('posts', PostController::class);
});
Conclusion
Laravel 8 refines development experience with modern factories, powerful job batching, and flexible rate limiting.
Upgrading to Laravel 8? Contact ZIRA Software for migration assistance.