Pro tip: Display all validation errors in Blade View

Every Blade View have $errors variable bounded to it unless you set different one by yourself. If you use controller based error handling, you will probably have something like this inside it:

We passed validation errors to the profile view using withErrors() method and inside that view you can display them:


Laravel: Model Observers

In the last tutorial I showed you how you can speed up a web application by caching database queries that are executed very often. However, there is a problem. Application will cache the resulted dataset, and in the future this dataset will be served. You need to tell your application to flush the cache every time when certain Model is updated. That way every time you edit, delete or add new Model item, old cache will be flushed, and query will be executed again. Afterwards, resulting new dataset will be cached and served until Model is updated once again. Continue reading Laravel: Model Observers

Laravel: Caching Database Queries

If your application runs a large number of queries very often, with time it will become very, very sluggish. Here Laravel caching comes handy. Laravel provides a simple mechanism for caching these queries using a very simple chained method call. Here is an example using Laravel’s Fluent Query Builder:

Of course, we can do the same thing using Eloquent:

Behind the scene Laravel executes the query and then stores it along with the query result using the cache adapter, with an expiration time of 60 minutes. Running the same query again will result that cached query will be found, which means it will not be executed again, instead the results will be taken from the cache. Continue reading Laravel: Caching Database Queries

Setting Up Laravel Environments

Setting up a development environment is an important task you should do before starting any project. We all know that Laravel has a lot of cool features and one of things I like a lot is how easy is to set environments on your machine. Official documentation page provides you a lot of useful information about it, so make sure you check it.

Typically for each project you would need to have development, staging and production environments. All of these should be on separated servers, so configuring each environment can be a bit tricky.  But not with Laravel. Continue reading Setting Up Laravel Environments

Laravel Filters: Route access depending on the user group

Filters are one of the Larevel’s features that I like the most. It’s pretty straightforward – you need to run some chunk of code before or after some action. Recently I had to implement restrictions on route resources depending on user group. I use Sentry for user authentication and role management and this Laravel filter should check if user belongs to specified group and allow him access to route resource if he does.

One solution is to simply create a new filter for every group. But, I don’t like repetition. I wanted simple and elegant solution that meets my requirements, without unnecessary repetition of code. Here is solution I came up with:

Continue reading Laravel Filters: Route access depending on the user group