Neon Tsunami

A blog on Laravel, Rails & Swift

By Dwight Conrad Watson

Escape string interpolation in ES6

I ran into an issue with string interpolation in ES6 and Vue.js today where it was trying to interpolate something that I wanted to be left as it was. I wanted to show a dollar sign followed by a Vue.js binding which looks a lot like the string interpolation syntax: ${}. `<span>${{ coupon.amount }}</span>` However, escaping this interpolation is easy - you simply add a backslash \ in front of either the dollar symbol $ or the opening brace {. // Before the $ `<span>\${{ coupon... Read more...

Mounting a Vue instance to a DOM element

Here's something cool I learnt from Evan You's talk on Vue.js at Laracon - how to mount a Vue instance to a DOM element after you've created it, instead of providing the el option in the constructor object. const vm = new Vue({ data: { name: 'Application' }, template: '<h1>Welcome to {{ name }}</h1>' }).$mount('body') The $mount() method on your Vue.js instance will then mount it on the DOM for you. The above code is the equivilant of the more familiar way. const vm = new... Read more...

Flux with CoffeeScript

Since I've been using React with Rails we've adopted the Flux pattern from Facebook which has a unidirectional data flow from stores down through your components. However where I work we also use CoffeeScript, and while there are a heap of resources for just getting started with Flux, they're seemingly always in ES5/ES6. So after some playing around and tweaking, here's my basic approach to Flux with CoffeeScript. class MemberStore extends EventEmitter constructor: -> @data = {} @d... Read more...

Versioning all assets with Laravel Elixir

One Laravel application I work on has a number of different asset pipelines set up for scripts and stylesheets - we have the public site, a secured content management system and also a view that we fit inside a mobile application. We run all these assets through Elixir and then version them so we can cache them better in production. Previously we explicitly told Elixir of all the assets we wanted it to version. It was a bit unwieldly. mix.version([ 'public/css/app.css', 'public/js/app.js',... Read more...

Runing a single test case in PHPUnit

Sometimes when you're trying to nail down a failing test you need a quick way to run just the thing thats broken, instead of your whole suite or a whole test case. Fortunately it's pretty simple with the --filter flag provided by PHPUnit. class PagesControllerTest extends \TestCase { /** @test */ public function it_displays_index_page() { $this->visit('/'); } /** @test */ public function it_displays_about_page() { $this->visit('/about'); } /** @t... 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

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 easy it is to have Laravel support read connections.... 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