Welcome to Netmechanics

Coding, Design & Thinking

Why Encrypt

The shape and structure of the internet is evolving, often conceived of and built by profit making companies, and at the same time the internet is increasingly considered an essential part of the machinery our society runs on.

Created: 28th Jul '15 ~ Last updated: 28th Jul '15

Sublime shortcuts

My notes on my favourite Sublime short cuts and features. Hard to know where to start considering how good it is.

Created: 25th Jul '15 ~ Last updated: 28th Jul '15

Find and replace across an entire Mysql database

Having to search through a database table and replace urls, content etc is something I have to do every week or two. I normally use the 'replace' command in SQL. Sometimes it is nice to check across an entire database and that requires a hefty SQL statement.

Since I've started using Mysql Workbench to manage my server databases via SSH I can now use a cool feature called 'Search table data', all you need to do is shift select the tables you wish to search in the sidebar, right click and select 'Search table data' and you are away.

Created: 23rd Jan '15

Cactus Specsystem

In the last half of 2014 we rebuilt Specsystem, a private manufacturing/planning (MRP) web application which Gwilym created over ten years ago for Cactus Climbing Equipment.

The old version was stuck together with PHP3, sticky tape and chewing gum, but still proved itself of great value in managing the Bills of Materials for products and scoping/planning production.

Created: 28th Nov '14 ~ Last updated: 11th Dec '14

Protecting part, or whole of Laravel site with SSL on Nginx

There is some argument about if doing it in the Nginx or in Laravel is the proper way. This is a tidy way to do it in Laravel. This assumes SSL is actually working fine (tested by going to https://www.domain.com):

In routes.php

Route::when(admin/'*', 'secure');

In filters.php

Route::filter('secure', function () {
    if (! Request::secure()) {
        return Redirect::secure(
            in_array(Request::getMethod(), ['POST', 'PUT', 'DELETE']) ? 307 : 302

To redirect all traffic to SSL in filters.php use something like this, wrapped to check for environment:

if (App::environment('production')) 
        if( ! Request::secure())
            return Redirect::secure(Request::path());

This worked well for everything except when arriving at the root non-ssl domain i.e. http://www.domain.com - to automatically redirect to SSL I amended the Nginx server block:

server {

    listen       80;
    server_name  xyleap.com www.xyleap.com;
    return       301 https://www.xyleap.com$request_uri;


Created: 21st Nov '14 ~ Last updated: 21st Nov '14

Search with Laravel and Eloquent

It is very common to need ways to easily filter and find database records in a web app frontend. A classic example is finding a user in a table once the number of records gets up in number.

Eloquent makes this very easy.

I've found the easiest way to use the pagination in conjunction with search is to make the search form return as Get instead of Post. It means the the search vars end up in the url, but it plays well with Laravel's in built pagination and cuts down on the controller methods and simplifies the views.

// Start building the query
$query = ProductionOrder::orderby('id', 'DESC');

// Status
if(Input::get('status') != '' AND Input::get('status') != 'all') {
    $status = Input::get('status');
    $query->where('status','=', $status);

// Search the title test
if ( Input::get('search') != '' ) {
    $search = Input::get('search');
    $query->where('title', 'LIKE', "%$search%");

// Search related title
if ( Input::get('search_assembly') != '' ) {
    $search_assembly = Input::get('search_assembly');
    $query->whereHas('Assembly', function($query2) use ($search_assembly) { $query2->where('title', 'LIKE', "%$search_assembly%"); });

// Executes the query and fetches it's results
$production_orders = $query->paginate(60);

Created: 7th Nov '14 ~ Last updated: 8th Dec '14

Seeding and insert speed in Laravel

When you start adding rows in the hundreds or thousands with Eloquent in Laravel you quickly realise that there is a difference in inserting line by line or in one big query. It's worth remembering that some versions of mySQL/SQL limit a single insert to 1,000 so chunking an array into inserts of < 1000 is the way to go.

This blog post here has a good summary - http://bensmith.io/speeding-up-laravel-seeders

Created: 31st Oct '14 ~ Last updated: 31st Oct '14

Laravel lists method for select lists

I often use the Laravel ->lists() method to build select lists for forms.

Usually I do something simple like the following to build a list of model User email addresses with the User key:

User::lists('email', 'id);

It's handy to be able to prepend an '--all--' option to the list, I've been just adding to the array

 $list = User::lists('email', 'id);
 $list = array('--all--', 0) + $list; 

Another handy thing to do is concatenate two field together for the label part of the option list:

User::select('id',  DB::raw('CONCAT(firstname, " ", lastname) as thename'))->orderBy('lastname', 'asc')->lists('id', 'thename');

I normally then feed this into a Select2 select that I build with a Form helper.

Created: 23rd Oct '14 ~ Last updated: 23rd Oct '14

Beanstalk with Laravel Queues

Queues in Laravel are really handy if you want to fire a lot of processes and are hitting the limit of what you can reasonably ask PHP to do.


  • Requires Beanstalk to be installed and adding pda/pheanstalk to composer
  • I couldn't get Supervisord managing Beanstalk on Ubuntu 14.04, or track down why it wasn't working
  • I'm planing on trying Upstart when I have time

This starts beanstalk:

sudo service beanstalkd start

This command (from your Laravel dir) starts to listen with an extended timeout:

php artisan queue:listen --timeout=600

This one just processes the first in queue:

php artisan queue:work

This is what I'm going to try:

## Instead of supervisord, why not use Ubuntu's native upstart? Just add the following to /etc/init/myqueue.conf:

description "work queue"
start on filesystem
stop on runlevel [!2345]
respawn limit 5 2
exec /usr/bin/php /path/to/laravel/artisan queue:listen
end script

## Then start the queue: "start myqueue". That's it, it will be automatically monitored and restarted in case of crash. No need for external daemons.
## Path to artisan i.e /srv/www.www.website.com/artisan/ ??

Created: 22nd Oct '14 ~ Last updated: 22nd Oct '14

Useful Mysql/SQL commands

Here are some of my favourite MySql commands:

This one takes the value of col2, splits it and inserts the second character into col1:

UPDATE table_name SET col_1 = SUBSTR(col_2, 2);

This is another handy one which inserts from one table into another:

column1, column2..
SELECT column1, column2, ..
FROM table1;

Created: 20th Oct '14 ~ Last updated: 20th Oct '14

Moving stuff around the web with Rsync

After marvelling at the wonders of FTP, and the ability to move large and numerous files around the internet, you quickly realise there might be a better way. One which syncs files quickly and efficiently between servers and is a little more intelligent. Well there is - Rsync and it is an excellent tool in the web developers tool box, on Linux of course.

Now, I now there are even better tools than rsync for deploying, Git for instance, that harness the power of version control. And I use those too. It's just for moving a large Wordpress images folder from one server to another, or a large MySQL dump, I've found Rsync to be incredibly useful.

To move from my local up to a certificate SSH enabled server, excluding node and git folders:

rsync -avz -e "ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null" --progress --exclude 'node_modules' --exclude '.git' ./ [email protected]_address_here:/srv/www/etc/etc/here/

Created: 11th Oct '14 ~ Last updated: 29th Oct '14