Laravel Tips and Tricks - June Edition

Jul 7, 2024

Laravel Tips and Tricks - June Edition

Introduction

  • Continuation of a tradition started last month to compile tips from Twitter about Laravel.
  • June edition with 18 random tips from Twitter, personal experience, and the community.

Tips

Tip 1: Redirect with Fragment

  • Redirect with fragment: Use the hash symbol in the URL to push the browser to a specific anchor.

Tip 2: Cache Headers on the Fly

  • Cache headers: Use the cache header middleware.
    • Example: public max age.

Tip 3: Artisan Commands from Functions

  • Run Artisan commands: Call Artisan commands directly from other functions.
    • Example: Artisan::call('command:name');

Tip 4: Redirect to Specific Route Helper

  • Shorter route helper: to_route helper for redirecting to a specific route.
  • Available since Laravel 9.

Tip 5: Array Functions vs. Collections

  • Use array functions: Opt for PHP array functions instead of Laravel collections when possible.
    • Example: array_column vs. Collection::pluck.
  • Reference: Course on PHP for Laravel developers.

Tip 6: New Collection Methods in Laravel 11

  • Collection methods: New methods before and after in Laravel collections.

Tip 7: Readable Method Names

  • Readable methods: Create methods to hide implementation details.
    • Example: Use isContentUnlocked instead of repeating conditions.
  • Follows DRY (Don't Repeat Yourself) principle.

Tip 8: Inline dd in Eloquent

  • Inline debugging: Use dd on the same line in Eloquent.
    • Example: Model::where(...)->dd();

Tip 9: Formatting to Percentages

  • Number formatting: New percentage method in Laravel 10's number class.
    • Includes locale formatting.

Tip 10: Controller Method Visibility

  • Controller method visibility: Use only and except in routes for public and private methods.
    • Example: Split the controller across route groups.

Tip 11: Progress Bar for Artisan Commands

  • Progress bar: Show live update progress bar for commands using Bus batch and progress bar.

Tip 12: Service Classes

  • Service classes: Simplified explanation of service classes for reusability.
    • Reference: Martin's thread on service classes.

Tip 13: Multi-Value Match in PHP

  • PHP multi-value match: Use match with multiple values to replace complex if-else structures.

Tip 14: Upcoming Array Methods in PHP 8.4

  • PHP 8.4 arrays: New methods like array_find, array_find_key, array_any, and array_all.

Tip 15: Prevent N+1 Query Problem

  • Prevent N+1 queries: Utilize preventLazyLoading() with custom handling in production.
    • Example: Log or notify instead of throwing an exception.

Tip 16: Multiple Ways to Call View

  • Call view: Lots of options in Laravel to call a view and pass data (array, compact function).

Tip 17: Discussion on Flexibility vs. Consistency

  • Framework flexibility: Discussion on Laravel's flexibility as both a feature and a downside.
    • Reference: Reddit discussion on switching from Laravel to Rails.

Tip 18: People to Follow for Laravel Tips

  • Follow for more tips: List of people to follow on Twitter for Laravel tips available in Laravel Daily resources.

Conclusion

  • Feedback requested on continuing this format for future months.