(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!
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.
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:
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!
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.