In Brief #003: MySQL Backups On Forge

How to use a simple cron job to generate nightly MySQL backups on Laravel Forge.

3 minute read

(Update: Forge auto-magically redirects cron job output to a job specific file, which means my cronjob below doesn’t quite do what is expected. I’ve written a new post about how this affects this strategy because it made more sense than trying to correct/update this one.) Shifting gears for a second, let’s chat about backups. I’m using Laravel Forge to run deployments on top of Linode servers. Both are ah-may-zing!

In Brief #002: Updating Rows In A Migration

A quick look at how to use a Laravel migration to populate values for a new column after using ALTER.

2 minute read

Laravel Migrations are a great way to manage your database tables. If you add Doctrine to your composer dependencies, then you can easily alter a table’s columns, indices and loads of other properties, without changing the way you write migrations. Sometimes, when you alter a table to add a new column, you want to populate it with values. There’s a couple ways to do this for basic operations. If your column will allow a default value, you can add it as part of the migration itself $table->string(‘colName’)->default(‘foo’); You can use a Seeder to insert rows, or to run just about any query you’d like.

Adding Tags In Eloquent With Cartalyst

Cartalyst has a great tags package for your Laravel projects, but there's a couple tricks to make using it feel seamless.

5 minute read

When I was first building out the code for this blog, I gave some thought to how to add tagging. The Laravel Docs actually use tagging as an example of polymorphic relations. But then, over on his new jam Makers Clique, Scrivs mentioned a package called Cartalyst Tags which looked like it ticked all the boxes. After following the installation instructions, I added a tags field to my post forms:

In Brief #001: Routing With CORS

CORS can be aa bit of an enigma to set up. Laravel makes it even more mystical. Here's a decoder ring.

2 minute read

If you’re building an API for your app, chances are you have to build in support for CORS. Doing so in Laravel is pretty straightforward, especially if you snag the laravel-cors package from Barry van den Heuvel. After adding laravel-cors to your project, you can just add route code like any of these: Route::post(‘train’, function() { return ‘Choo, Choo!’; })->middleware(‘cors’); Route::post(‘boat’, [‘middleware’ => ‘cors’, function() { return ‘Hooooonk!’; }]); Route::group([‘middleware’ => ‘cors’], function($router){ $router->post(‘car’, function() { return ‘Meep, Meep!

Adding More Robust Ajax Detection In Laravel

Laravel 5.2 (and earlier) ships with ajax detection that is unreliable. Here's a fix that gets almost complete coverage.

7 minute read

TL;DR The $request->ajax() method does not detect XHR sent without an X-Requested-With header, so you’ll want to add the following to your own Request.php file (or whatever base Request class is used by your controllers). App/Http/Requests/Request.php <?php namespace App\Http\Requests; use Illuminate\Foundation\Http\FormRequest; abstract class Request extends FormRequest { public function ajax() { /* 1. Call the builtin method / if ($this->isXmlHttpRequest()) { return true; } / 2. Then check the Content-Type / $content_type = $this->header(‘Content-Type’); $allowable_types = array( ‘application/json’, ‘application/javascript’, ); if (in_array( strtolower($content_type), $allowable_types)) { return true; } / 3.

v0.2