Neon Tsunami

A blog on Laravel, Phoenix & Rails

By Dwight Conrad Watson

Multiple database read/write connections in Laravel

This post follows up on my previous post Multiple read-only database slaves in Laravel after I got a helpful comment from Ezequiel Russo who showed me an even better way to use multiple database read/write connections in Laravel - one that works with Laravel's config caching. It turns out that Laravel will automatically pick a database connection at random if provided with an array, so there's no need to pick a random connection yourself. This makes it really easy to define your connections and... Read more...

Using Laravel Mix on Heroku

Setting up Laravel Mix on Heroku is very similar to how you'd set it up with Elixir. Because Laravel puts your front-end dependencies in devDependencies instead you just need to tweak the default configuration. First, set YARN_PRODUCTION to false using the following command. This tells Heroku to install the devDependencies in your package.json, but leaves NODE_ENV as production. $ heroku config:set YARN_PRODUCTION=false Next, set the engines in your package.json. Make sure you have yarn at leas... Read more...

Changing the base URI with Laravel 5.4 testing

While upgrading my app from to Laravel 5.4 and replacing my "BrowserKit" style tests with the new evolution TestCase I ran into some trouble testing routes that came through on subdomains. The tests that used to pass would now 404. Previously, I used this at the top of a test case to set the base URL (my API endpoint) so that the tests would run off it. /** * The base URL to use while testing the application. * * @var string */ protected $baseUrl = 'http://api.neontsunami.com'; Ho... Read more...

Using Laravel (and Laravel Elixir) on Heroku

I've just started playing with Heroku for some of my Laravel projects as they support PHP 7, and it seemed like Laravel was almost ready to run out of the box. In fact, there's a great get started guide for using Laravel on Heroku and it couldn't be much simpler. However, I do have some tweaks to Heroku recommend changing your logger in config/app.php to errorlog so that the app errors are made available through the command line tools. Instead of changing the file you can just set the APP_LOG en... Read more...

Using jQuery and plugins with Browserify/Webpack

One thing that caused me a bunch of issues when moving from concatenated scripts to a package bundler with ES6/ES2015 modules like Browserify and Webpack was getting jQuery to play nicely globally with things like Bootstrap and plugins like Selectize. Because of the way modules work it can be a real pain to get everything working again the way you'd expect. The way I ended up getting it working was to bootstrap jQuery it in's own file, a seperate module from my root app JavaScript file and then... Read more...

Read more recent posts

Most popular posts

Timezones in Laravel 4

Here's something cool I didn't know about timezones in Laravel 4 - instead of just providing an offset from UTC you can use the default timezones as provided by PHP. In your config/app.php file, you've got the following setting by default: /* |-------------------------------------------------------------------------- | Application Timezone |-------------------------------------------------------------------------- | | Here you may specify the default timezone for your application, which | will b... Read more...

Using lists() in Laravel with custom attribute accessors

Sometimes when you're listing out a model for a select box or another form of displaying your data, you'll want to use a custom format for the list and keep the key as the row ID. You might want to get your collection first and then loop through it to create the array in the format you need, but there is a better way. For example in a project I'm working on we want to list out all the subjects of a university displaying both the subject code and it's name. The way we go about this is to define a... Read more...

Laravel redirect to intended location after login

Interesting tidbit I discovered over the weekend when using the auth filter in Laravel 4 to redirect users where they had originally intended to go. Before I had found this, I was passing the intended location encoded in the URL and then picking it up in the login form, which in hindsight was pretty darn silly. Route::filter('auth', function() { if ( ! Auth::check()) return Redirect::guest('login'); } If you actually look at the definition of the guest function of the redirector, you'll see... Read more...

Multiple read-only database slaves in Laravel

See an updated post on multiple read-slaves here. Having recently moved one of our applications to AWS I've discovered how easy it is to create read-only database replicas if you're using the RDS service. RDS makes it really easy to provision an optimised database server for your application and supports a range of engines out of the box, including MySQL, Postgres and a new highly-optimised MySQL fork called Aurora. Because it's now a cinch to create a database slave I was excited to see how eas... Read more...

Laravel 4.1 Blade "if isset" shorthand

Laravel 4.1 introduces a new shorthand for echoing out data in Blade, which is really nice. {{ $variable or 'default' }} This shorthand actually expands out to a full isset call on the variable and returns the default value if false. isset($variable) ? $variable : 'default' So if you're not sure if something is going to appear in the view or not, here's a nice way to test! Read more...

Read more posts