laravel/laravel — pull request #1853 by cviebrock (2013-04-03 23:42:28+00:00)
Title: Add Macros to tables

Here's the theory behind this:

Let's say I wanted to create a bundle that allowed for adding timestamps to an eloquent model. Yes, Laravel already does it, but pretend it didn't. As of now, you'd need to give instructions to users to create the two new columns in their table, make sure they named them correctly and had the right column type, etc..

By allowing table macros, you can do all the hard work for them. In the fictional Timestampable bundle's `start.php` file, you could include code like this:

```php
Laravel\Database\Schema\Table::macro('timestampable', function($table) {
$table->date('created_at');
$table->date('updated_at');
});
```

Then, the user only needs to do this in their migrations:

```php
Schema::create('foo', function($table)
{
$table->increments('id');
$table->string('name');
$table->timestampable();
});
```

Of course, you would also create a `Timestampable` trait that would manage the setting and updating of those values on `save()` ... but that's not important here.

So, the main reason is to allow bundle authors the ability to do custom schema declarations, without pushing the complexity to the end user. As a more complex example, consider:

```php
Laravel\Database\Schema\Table::macro('uploader', function($table, $file) {
$table->string($file.'_name');
$table->string($file.'_original_name');
$table->integer($file.'_size');
});

...

Schema::create('user', function($table)
{
$table->increments('id');
$table->string('name');
$table->uploader('avatar');
});
```