Laravel remains one of the most popular PHP frameworks. Here are modern development practices for building scalable Laravel applications.

1. Project Structure

app/
├── Http/
│   ├── Controllers/
│   ├── Middleware/
│   ├── Requests/
│   └── Resources/
├── Models/
├── Services/
├── Repositories/
├── Events/
├── Listeners/
└── Jobs/

2. Eloquent Best Practices

Model Relationships

// User Model
class User extends Model
{
    public function posts()
    {
        return $this->hasMany(Post::class);
    }
    
    public function profile()
    {
        return $this->hasOne(Profile::class);
    }
    
    public function roles()
    {
        return $this->belongsToMany(Role::class);
    }
}

Eager Loading

// Bad: N+1 problem
$users = User::all();
foreach ($users as $user) {
    echo $user->posts->count(); // Query for each user
}

// Good: Eager loading
$users = User::with('posts')->get();
foreach ($users as $user) {
    echo $user->posts->count(); // No additional queries
}

Query Scopes

class Post extends Model
{
    public function scopePublished($query)
    {
        return $query->where('status', 'published');
    }
    
    public function scopeRecent($query)
    {
        return $query->orderBy('created_at', 'desc');
    }
}

// Usage
$posts = Post::published()->recent()->get();

3. Service Layer Pattern

Service Class

class UserService
{
    protected $userRepository;
    protected $emailService;
    
    public function __construct(
        UserRepository $userRepository,
        EmailService $emailService
    ) {
        $this->userRepository = $userRepository;
        $this->emailService = $emailService;
    }
    
    public function createUser(array $data): User
    {
        DB::beginTransaction();
        
        try {
            $user = $this->userRepository->create($data);
            $this->emailService->sendWelcomeEmail($user);
            
            DB::commit();
            return $user;
        } catch (\Exception $e) {
            DB::rollBack();
            throw $e;
        }
    }
}

4. Form Requests

Validation

class CreateUserRequest extends FormRequest
{
    public function rules(): array
    {
        return [
            'name' => ['required', 'string', 'max:255'],
            'email' => ['required', 'email', 'unique:users,email'],
            'password' => ['required', 'string', 'min:8', 'confirmed'],
        ];
    }
    
    public function messages(): array
    {
        return [
            'email.unique' => 'This email is already registered.',
            'password.min' => 'Password must be at least 8 characters.',
        ];
    }
}

// Controller
public function store(CreateUserRequest $request)
{
    $user = $this->userService->createUser($request->validated());
    return new UserResource($user);
}

5. API Resources

Resource Transformation

class UserResource extends JsonResource
{
    public function toArray($request): array
    {
        return [
            'id' => $this->id,
            'name' => $this->name,
            'email' => $this->email,
            'created_at' => $this->created_at->toIso8601String(),
            'posts' => PostResource::collection($this->whenLoaded('posts')),
        ];
    }
}

6. Queues and Jobs

Job Implementation

class SendWelcomeEmail implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    
    public function __construct(
        public User $user
    ) {}
    
    public function handle(EmailService $emailService): void
    {
        $emailService->sendWelcomeEmail($this->user);
    }
    
    public function failed(\Throwable $exception): void
    {
        // Handle failure
    }
}

// Dispatch
SendWelcomeEmail::dispatch($user);

7. Events and Listeners

Event System

// Event
class UserRegistered
{
    public function __construct(
        public User $user
    ) {}
}

// Listener
class SendWelcomeEmail
{
    public function handle(UserRegistered $event): void
    {
        Mail::to($event->user->email)->send(new WelcomeMail($event->user));
    }
}

// Dispatch
event(new UserRegistered($user));

8. Caching

Cache Implementation

// Cache user
$user = Cache::remember("user.{$id}", 3600, function () use ($id) {
    return User::find($id);
});

// Cache tags
Cache::tags(['users', 'posts'])->put("user.{$id}", $user, 3600);

// Clear cache
Cache::tags(['users'])->flush();

9. Database Migrations

Migration Best Practices

Schema::create('users', function (Blueprint $table) {
    $table->id();
    $table->string('email')->unique();
    $table->string('name');
    $table->timestamp('email_verified_at')->nullable();
    $table->string('password');
    $table->timestamps();
    $table->softDeletes();
    
    $table->index('email');
});

10. Testing

Feature Tests

class UserTest extends TestCase
{
    use RefreshDatabase;
    
    public function test_can_create_user(): void
    {
        $response = $this->postJson('/api/users', [
            'name' => 'John Doe',
            'email' => '[email protected]',
            'password' => 'password123',
            'password_confirmation' => 'password123',
        ]);
        
        $response->assertStatus(201)
            ->assertJsonStructure([
                'data' => [
                    'id',
                    'name',
                    'email',
                ]
            ]);
        
        $this->assertDatabaseHas('users', [
            'email' => '[email protected]',
        ]);
    }
}

Best Practices

  1. Use Service Layer for business logic
  2. Form Requests for validation
  3. API Resources for data transformation
  4. Queues for long-running tasks
  5. Events for decoupled actions
  6. Eager Loading to avoid N+1 queries
  7. Caching for performance
  8. Write Tests for critical paths

Conclusion

Laravel provides powerful tools for building modern applications. Follow these practices to create maintainable, scalable Laravel applications! 🚀