Back

Binding user ID in Laravel requests without the headache: Extend FormRequest, override prepareForValidation(), and merge the user()->id automatically. This way, no need to pass user_id in every request manually! 🎉


image
image

2

130

In response to @cebozkurt

In what cases would you prefer that over calling `auth()->user()` in the Controller?

1

150

  • No matching results...
  • Searching...

/ 1000

My approach ensures cleaner controllers and allows for logic reuse across various parts of my API project at least from my perspective :D. It's perfect for consistently binding and validating the user ID.

For simpler cases where it's only needed in one place, I directly use auth()->user() within the controller.

167