Reusable Validation Rules with Laravel Form Requests

Currently I'm working on a project with a lot of forms that have repeated form fields. For example, pretty much all entities in the system can have attachments, so a lot of forms contain the same attachment fields. I could repeat the same validation rules in all form request classes, but this quickly breaks down. What if the rules for attachments change? Let's see how we can handle this more efficiently.

Read more

Testing Your Laravel Middleware

While working on an application I wanted to test my middleware. When doing some investigation on this issue I didn't find any satisfying solutions, most involved manually creating a Symfony request object or mocking the request entirely.

Read more

Writing an Allowed Username Validator in Laravel

When building a project that requires top-level public user profiles, you will have to find a way to prevent the usernames from clashing with public URLs or files. If you don't do this, it's only a matter of time before someone signs up with the username 'logout' and make everyone who clicks on the profile link logout of their account. Here's a solution that I came up with that allows you to build it once and forget about it for the rest of the project lifetime.

Read more

Easy Bootstrap Forms in Laravel

When you are working with Bootstrap in Laravel, the extra div's that are required to format the form properly can really get in the way of the readability of your template. In this post we take a look at how to make it easier to manage your Bootstrap-based forms.

Read more

Upgrading Legacy Passwords With Laravel

I recently began to rebuild a legacy application using Laravel. I soon ran into a problem: the passwords stored in the database were using an old SHA hashing mechanism. I didn't want to bother existing users to enter a new password, so I wanted to easily upgrade the passwords without causing these users any trouble. The problem is that you can't backtrack their passwords, so just rehashing them isn't an option. How do you go about doing this? Let me show you what I came up with.

Read more

Implementing A Page View Counter In Laravel

Implementing a page view counter into your Laravel application seems like the easiest job at first. Just add a column in your database with the current view count and increment that on every page load, right? Well, there's a little more to it: This will cause the counter to increment on every page load, even when the user refreshes the page or subsequently visits the page in a short amount of time. This usually isn't the desired behavior, so let's look into solving this issue. In particular, we will be making use of Events and Route Filters to achieve this. Let's walk through the process of creating this using an incremental approach.

Read more

Extending The Connection Class In Laravel

Sometimes you may wish to extend the database Connection class in Laravel. Maybe you want to customize the internal workings. In my case I needed to alter the database results for every query, whether it's through the Fluent Query Builder or Eloquent. I needed to do this to make my package, Laravel DB Normalizer, work as smoothly as possible.

Read more