Neon Tsunami

A blog on Laravel & Rails

By Dwight Conrad Watson

115 posts found with this tag.

Using date where helpers with Laravel's query builder

Laravel recently introduced a number of query builder helpers for writing where clauses that act upon dates. There's a whole host of them, including whereDate(), whereTime(), whereDay(), whereMonth() and whereYear(). These helpers allowed me to clean up some older code that was relying on database specific date functions. Here are some extracts from this code which uses whereRaw and MySQL date functions to filter timestamps based upon their year. You can see that one example uses interpolation (... Read more...

.gitignore for Laravel Mix build artifacts

Many Laravel developers will build their front-end assets locally before deploying a new release and the framework ships out-of-the-box to support this. However sometimes you may rather to build your assets as part of your CI process or on production itself. If you're going to be building your production assets elsewhere then you don't want your repo tracking the assets you create during development. There's no point filling it up with files that can be rebuilt easily at any time, and a public/m... Read more...

Laravel auth routes without registration

Laravel's authentication scaffold is a great way to get your app off the ground quickly - both because of the time it saves you from the get go but also because it's a common implementation other Laravel developers are familiar with. However there may be times you want authentication but you don't want people to be able to register for your site. It might be a private back-end that you'll want to create users for manually, or you may have internal tools for inviting new users. Luckily it's prett... Read more...

Autologin with Laravel 5.6

Automatically logging in your users when they come back to your app is a great way to keep them engaged. If you can prevent distracting them from the authentication flow where they may have forgotten their password, or even the email address they used to join your site then they can get on with doing what they intended and they'll be happier customers for it. The most common use-case for this I've worked with is for customer notifications. Whether it be email or text, if you're sending them a pr... Read more...

Hooking up a CDN with Laravel cloud storage (Flysystem)

I've recently finished converting all our legacy S3 storage code into leveraging Laravel's out-of-the-box Flysystem integration. I was able to replace a tonne of S3 specific code with some really small service classes that generate thumbnails and so on. While doing this I learnt a few new cool things about this integration and also how to configure it with a CDN. Previously I had a heap of methods for building the links to files I stored on S3. This is so much easier when using the built-in help... Read more...

Ignoring Moment.js Locales with Laravel Mix

Following up from an earlier post that described filtering out Moment.js locales with Rails Webpacker, here’s how you can achieve the same thing using Laravel Mix. It’s worth dong for the same reason - filtering out a heap of locales you’re unlikely to use - and it works just the same. Here’s how you configure it in your webpack.mix.js file: const webpack = require(‘webpack’); const { mix } = require(‘caravel-mix’); mix.webpackConfig({ plugins: [ new webpack.IgnorePlugin(/^\.\/locale$/,... Read more...

Eloquent model without updated_at (Laravel 5.5)

It’s changed a little through the different versions of Laravel 5, but it looks as though Laravel 5.5 has just received an official way to disable the updated_at timestamp on your Eloquent models. PR #21178 allows you to set a constant on your model which instructs Eloquent to not attempt and set the timestamp. class User extends Model { // Do not set updated_at timestamp. const UPDATED_AT = null; } Read more...

Element is not clickable at point (Laravel Dusk)

Ran into an interesting issue today when filling in a form with Laravel Dusk. It ran locally but then failed selecting an input when running on CircleCI. It was quite a long form on the page, so by the sounds of the issue the form element I wanted to select was out of the viewport and for that reason Chrome couldn’t interact with it. Facebook\WebDriver\Exception\UnknownServerException: unknown error: Element is not clickable at point (988, 1124) (Session info: headless chrome=61.0.3163.79) (... Read more...

Scaffolding Laravel tests

I’ve been a big fan of forced resourcefulness – that is to ensure your controllers map to a single resource only and have exposed at most the 7 resourceful actions; index, create, store, show, edit, update, destroy (or similar, depending on what framework you’re using). There are many benefits to writing your apps this way, including keeping your code organised, your classes shorter and cleaner and easier to navigate. In addition it also allows you to scaffold the CRUD logic relatively easy. Not... Read more...

Overriding Laravel authentication controllers

I spent a couple of hours over the weekend re-writing the authentication controllers that run StudentVIP. They were written a long time ago in the early Laravel 4 days (and originally ran on the now-defunct Sentry package). It was time to bring them back into line with the default scaffolding that ships with Laravel to make it easier for everyone that works on the app. I noticed that the logout method doesn’t really provide any hooks for you to customise what happens. In my use-case we needed to... Read more...

Referencing /node_modules with Laravel Mix

Often when working on your front-end with Laravel Mix you’re going to want to import or reference some third-party code you’ve brought in through NPM/Yarn. Whether it’s JavaScript or some stylesheets, it’s painful and ugly to go through the whole ../../../node_modules/foo/bar/baz.xyz routine. Luckily, there’s a better way. JavaScript With JavaScript, Webpack will allow you to start off with the package name and then refer directly to the asset you’re after. In the following example I’m pulling t... Read more...

Testing the redirect URL in Laravel

In Laravel it’s quite common to return “back” to the previous URL a visitor was on. If you’re not using return back() or something similar this magic is often handled under the hood - either by a FormRequest or by use of the $this->validate() controller method. However, testing this can be a bit of a pain. If Laravel cannot work out what the previous URL was it will just redirect to the home page. This can throw off some feature tests - if you want to ensure an invalid form request takes you... Read more...

Laravel Auth scaffold tests

You've read it once, you've read it a thousand times - one of the hardest things when first getting into testing your code is knowing what to test and then how to test it. Especially once you throw a framework into the mix you need to get your head around quite a number of new APIs before you can get off the ground. What are the different kinds of PHPUnit assertions and how can I use them? What utilities does Laravel provide to assist testing and what are the conventions for their use? Now more... Read more...

Canonical URLs for Laravel

One thing I've had to do with most Laravel apps is handle the redirect from www. to non-www. or vice-versa. Many people may choose to handle this in their web server stack (where it's likely to be faster) but I generally prefer to handle this in the app. It makes the app more portable (this logic isn't tied to a web server configuration) and it's more accesible to the developers. However I noticed I was copy-pasting middleware over and then just changing the expected domain name. Because of this... Read more...

Laravel scheduler on Heroku

The Laravel scheduler is a great feature that removes the hassle of managing cron tasks, letting you handling everything in your app. Heroku is a great platform that relieves you of having to look after your own infrastructure. However getting the two to work together isn't quite perfect. Heroku offers the Heroku Scheduler add-on which can perform calls into your app on demand, however it is limited to running at most every 10 minutes. This might be fine if your schedule isn't particularly heavy... Read more...