Split char-separated values in MySQL

Introduction

Recently I needed to extract a list of URLs from a MySQL table. All those URLs were inserted in a single TEXT column, however they were all separated by semicolons. The situation was more or less like the following:

SELECT * FROM links

---------------------------------------------------------------
| id | urls                                                   |
---------------------------------------------------------------
|  1 | http://first.link;http://second.link …
Read More
Feature Flags in ProcessWire

Introduction

While building a website or an application, it’s often useful to control which parts are visibile (and which are not) to the actual users. Maybe you just want to hide a particular functionality on which you are still working, or maybe you just want to reveal things gradually without deploying new code in production. This concept is called Feature flags or Feature toggles.

Feature Flag is a technique to turn some functionality of your application off, via configuration, …

Read More
Using ProcessWire InputFieldDateTime via API

Introduction

Recently I had the need to create a custom form in the backend, using the API and InputField modules that ProcessWire provides. Think InputFields as the basic building blocks to create forms in ProcessWire. They are used all over the administration panel, and provide styling (via jQuery UI), validation ad error rendering (through the FormBuilder) out of the box.

Here is an example of how you can use the API to create a basic form. The code is taken from this PW forum thread …

Read More
Conditional validation on Laravel's Form Request

Introduction

In my opinion, Laravel Form Requests are one of the most useful components of Laravel. They can perform authorization and validation on requests, without even touching the controller.

Form Requests are also very flexible, because they are completely customizable by method overriding.

When creating a Form Request, the first method to implement is the authorization() method, which can be used to check if the authenticated user can perform the action. For example the following …

Read More
Confirm form submission without loops using jQuery

Introduction

This post falls in the category “Short tricks that I would teach to my younger self”.

Let’s assume that you want to display a confirmation dialog before submitting an important form. You can leverage the Window.confirm() method, which displays a modal dialog with two buttons, Ok and cancel.

Window.confirm() returns true if the user clicks on the Ok button.

<form onsubmit="return confirm('Do you really want to submit the form?');">

You …

Read More
Check for foreign key existence in migrations

Laravel provides a couple of handy features to check for the existence of a table or column using the hasTable and hasColumn methods:

<?php

if (Schema::hasTable('users')) {
    //
}

if (Schema::hasColumn('users', 'email')) {
    //
}

However, sometimes you need to check the existence of foreign keys or indexes on tables, for example when you need to rename columns, in order to avoid annoying errors. Unfortunately Laravel doesn’t provide such methods out of …

Read More