Published
09 Apr, 2025
Category
Case Study
10 Laravel Mistakes I Made (and How I Fixed Them)
When I started with Laravel, I thought it would be smooth sailing after composer create-project. Instead, I spent hours fighting silly mistakes. Looking back, those mistakes taught me more than any tutorial. Here are 10 of them and how I fixed each one.What to expect: Real-world errors, the exact fixes I used, and quick lessons you can apply immediately.

1) Leaving .env in public_html
  • What happened: I accidentally uploaded .env to the web root, making it publicly accessible.
  • Why it’s bad: Exposes DB credentials, API keys, and secrets.
  • Fix: Keep .env in the project root (outside public_html) and ensure it’s in .gitignore.
    # .gitignore .env
  • Lesson: Security first always check what’s public.
2) Forgetting APP_KEY
  • Symptom: Sessions/auth broke; error: “No application encryption key has been specified.”
  • Fix: Generate and cache the key.
    php artisan key:generate --force
    php artisan config:cache
  • Lesson: APP_KEY is foundational — set it before testing auth and sessions.
3) Not clearing config/cache after .env changes
  • Symptom: I edited .env but nothing changed in the app.
  • Fix: Clear caches after environment/config updates.
    php artisan config:clear
    php artisan cache:clear
    php artisan route:clear
    php artisan view:clear
  • Lesson: Laravel caches aggressively; clear first, then re-test.
4) Skipping storage:link (broken images/files)
  • Symptom: Uploaded files existed but wouldn’t display from the browser.
  • Fix: Create the public storage symlink.
    php artisan storage:link
  • Lesson: Public access to storage/app/public requires the symlink.
5) Mixing API and web routes
  • What happened: I put API-style endpoints in web.php, hit CSRF issues and middleware confusion.
  • Fix: Use the right files and middleware.
    // routes/web.php (session + CSRF)
    Route::get('/', [HomeController::class, 'index'])->name('home');
    // routes/api.php (stateless)
    Route::get('/posts', [PostApiController::class, 'index']);
  • Lesson: Keep web routes (CSRF/session) separate from stateless API routes.
6) Hardcoding URLs instead of helpers
  • Symptom: Moved from local to production and links broke.
  • Fix: Use route and asset helpers.
  • Lesson: Helpers make URLs environment-agnostic.
7) N+1 query hell
  • Symptom: Slow page rendering when looping related models.
  • Fix: Eager load relationships.
    $users = User::with('posts')->get();
    @foreach($users as $user)
    {{ $user->posts->count() }}
    @endforeach
  • Lesson: Learn to spot N+1 early; use with(), debug bar, and query logs.
8) Forgetting to run migrations
  • Symptom: “Base table or view not found” while testing features.
  • Fix: Run migrations (and seed if needed) before debugging logic.
    php artisan migrate --force
    php artisan db:seed --force
  • Lesson: Schema first, code second.
9) Putting too much logic in Blade
  • What happened: I crammed queries and conditionals into Blade, making views hard to test and maintain.
  • Fix: Move logic to controllers, services, or view composers.
    // Controller$posts = Post::published()->latest()->paginate(10);
    return view('posts.index', compact('posts'));
    // Blade (presentation only)
    @foreach($posts as $post)
    {{ $post->title }}
    @endforeach
  • Lesson: Keep Blade declarative; test logic in PHP classes.
10) Deploying without optimizations
  • Symptom: First production load was slow and inconsistent.
  • Fix: Add optimization steps to your deploy script.
    composer install --no-dev --optimize-autoloader
    php artisan config:cache
    php artisan route:cache
    php artisan view:cache
  • Lesson: Treat optimizations as mandatory in production.
Bonus: Where to look first when things break
  • Check logs: storage/logs/laravel.log
  • Clear caches: php artisan config:clear, cache:clear, route:clear, view:clear
  • Permissions: storage and bootstrap/cache must be writable
Outro

Mistakes are part of the learning curve. If you’re a junior like me, don’t be afraid of breaking things, just make sure you learn how to fix them.