Log Route calls in Laravel using Middleware

Introduction

Sometimes when developing a new site or application in Laravel you need to log exactly the route calls in order to track bugs and errors. This operation is straightforward.

First, let’s create a new Middleware

php artisan make:middleware LogRouteMiddleware

Then edit the handle function like the following:

<?php

public function handle($request, Closure $next)
{
    Log::info($request->fullUrl());

    return $next($request);
}

The fullUrl method logs the full …

Read More
Work on a Laravel Live Site while in Maintenance Mode

Introduction

Since Laravel 4 there’s a handy feature that allows you to put your site in maintenance mode, providing a good way to hide your changes from external eyes. This feature is really helpful on your testing or production server since it doesn’t allow users to browse the site, so you can make you modifications pretty much unnoticed.

The artisan commands are the following:

php artisan down

to put the site into maintenance mode, and

php artisan up

to disable the …

Read More
Keep URL and query parameters using manual pagination in Laravel

Introduction

The Laravel documentation is great most of the time, but when you are stuck on a particular problem you have to figure by yourself what is the solution. Recently I had to create manually a paginator instance using the LengthAwarePaginator class. The first attempt was nearly a success, here a snippet of my controller:

<?php

$page = $request->has('page') ? $request->get('page') : 1;

$data = $this->userRepository->paginate($request->all(), …
Read More
Database Design for Bus Timetables

Introduction

Recently, for a large website commissioned by the local agency for tourism, I had to design a system for storing Skibus timetables. The requirements were straightforward:

  • store multiple bus lines;
  • each line is a round trip, therefore it has two directions (outbound and return). But the route may not be the same;
  • each line is effective between two dates

The aim of the website was to provide timetables to regular users using standard HTML tables instead of static PDF files, …

Read More
Efficient appended property using Laravel Eloquent

Introduction

This technique is really useful to check if an Eloquent relation is loaded on a Model, in order to efficiently generate an appended property.

For example, this approach is particularly useful in a multilingual application, where you can have an entity that has a number of related translations in different languages. Consider the following code:

<?php

class Entity extends Model
{
	public function translations()
	{
		return $this->hasMany('Translation');
	}
}

Our …

Read More
Dropzone.js, React.js and Fluxxor integration

Introduction

In these days I’m digging deep in the wonderful React.js library, widely used by Facebook and Instagram for their UIs. This library uses an abstraction of the DOM, called Virtual DOM, to render components in the View. Honestly I’m was not a big fan of DOM abstractions, because in my opinon it affects both the work of developers and designers. HTML should not be mixed with JavaScript and viceversa. I’ve worked in the past years with declarative libraries like Knockout.js, so …

Read More