Neon Tsunami

A blog on Laravel & Rails

By Dwight Conrad Watson

109 posts found with this tag.

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

Building relations with Laravel model factories

Often when setting up your environment for tests you'll need to generate models that require certain relations otherwise they won't work properly. Usually a view will try and access the related models and throw an error when it can't find them but sometimes your core app logic will go wonky as well. // Manually creating the relationship tree. $user = factory(User::class)->create(); $series = factory(Series::class)->create(); // Note you can just pass an Eloquent model and Laravel grabs th... Read more...

Redirecting to HTTPS and/or www with Laravel

You're often going to want to redirect your root domain or the www. subdomain to one or the other, rather than having both point to the same site. If they both display the same site then Google will interpret them as duplicate content and it can affect your ratings. The same goes for serving your site over HTTP and HTTPS - you're best to perform a permanent redirect to the URL and scheme you want your customers to use. Generally it's faster to get this sort of thing up and running in your web se... Read more...

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

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

Speed up your Laravel tests by 70% with this one simple trick

Excuse the clickbait title of the post, but it does so happen that a simple change to my Laravel test suite was able to speed up total test run time from 165 seconds to just 45 seconds. It should have a helpful effect on your suite run time if you've got a similar setup too. My application uses the Illuminate\Foundation\Testing\DatabaseTransactions trait throughout the tests wherever database interaction is required. Because the app is quite large it made more sense to migrate the database befor... Read more...