Neon Tsunami

A blog on Laravel, Phoenix & Rails

By Dwight Conrad Watson

Nested route model binding in Laravel

I'm a big fan of route model binding in Laravel, and have been using it since it was first introduced. I was very excited when it later became standard with implicit route model binding in 5.2. There is just one issue that route model binding brings that is easily solved by the alternative, fetching models yourself in the action. Let's use an example of fetching a user's post. public function show($userId, $postId) { $user = App\User::findOrFail($userId); $post = $user->posts()->f... Read more...

Restricting models from a Laravel Scout index

For some implementations you may want to restrict some of your models from going into your Laravel Scout index. For example on my own blog I only want to index my posts which have been published - if for whatever reason I haven't decided to publish a post yet then it shouldn't be searchable. The solution below will work fine but in the current release of Laravel Scout (1.1.4) it will simply index your restricted models as an empty array. In the next tagged release though, it will no longer need... Read more...

Disabling Laravel Scout in test environment

Having just added Algolia Search to my blog with the help of Laravel Scout I found that my test suite started failing because the test environment didn't have access to my Algolia configuration. Laravel Scout also supports a "null" driver which would fit the bill perfectly, but theres a bit of an issue when trying to set the SCOUT_DRIVER to null using an environment variable. It seems like Laravel converts the string "null" to be literally null and then the Scout engine fails... Read more...

Testing pages that don't work in Laravel

Ran into a little bit of trouble today trying to test that a page didn't work in my Laravel app. I wanted to ensure that a user who tried to access a page they weren't authorised to view did in fact not get access to that page. Unfortuantely, using visit() as I normally do wasn't doing the trick. /** @test */ function it_rejects_unauthorized_users() { $this->be(new User); $this->visit('admin'); } A request to [http://example.com/admin] failed. Received status code [403]. This re... Read more...

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...

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