Top 5 Common Issues When Using Laravel
Oliver Sarfas • October 28, 2019
programming laravelLaravel is coveted as the most popular PHP web framework in the world right now, and for good reason. It's got a lot going for it with it's vast ecosystem of Laravel Packages, great communities, and exemplary documentation - what isn't there to love?
Well, it turns out that a lot of people - myself included, more times than I'll openly admit - bump into some simple issues when using it. 99% are down to developer error, so I'm going to reel off a few of those now. We'll go into the more complex common issues in a later post, as they'll need more attention.
So what are the most common errors that people see when using Laravel for the first time? Let's take a look.
No Application Key Set
On a fresh laravel clone, you'll get a .env.example
file. Now, if you've not used an installer CLI, you won't have generated your application key.
To generate an application key, simply run the following;
php artisan key:generate
From there your application will run as expected.
{table} not found
So you're on a page on your Laravel site, and you're getting a "table does not exist" or "table not found in database" error. This could be due to one of a few reasons. Here's the most common ones that you'll encounter when learning Laravel;
You haven't run the migration(s)
Run
php artisan migrate
to get your database up to date with all migrationsThere's a typo in your model
Sometimes you set a
protected $table
on your model, confirm that's spelt correctly. If you haven't set one, check the name of the Model itself, as Laravel will use some magic to guess the table nameYour database connection isn't quite right, or you're on the wrong connection
Check the
.env
file settings for your DB settingsCheck if a
protected $connection
has been set on the Model
Specified key was too long
This only happens in some of the older Laravel code bases, when moving to a more recent release. Specifically anything pre Laravel 5.4 moving to post-5.4
Open your
AppServiceProvider.php
file, and add the following;use Illuminate\Support\Facades\Schema; public function boot() { Schema::defaultStringLength(191); }
The reasoning for this is because the default charset for Laravel moved to support emojis (using the
utf8mb4
database character set)If you're running MySQL 5.5.7 or higher, you will not find this issue as it's handled at the database level 🎉
HTTP 419 Page Expired whenever submitting a form
99% of the time this is due to not submitting a
CSRF Token
with your request. There's two ways to resolve this;Add the
@csrf
to your blade file within the<form>
tagsAdd the URI to the
VerifyCsrfToken
middlewareprotected $except
array, so that the route no longer requires CSRF tokens
The stream of file {{some_file}} could not be opened: failed to open stream: Permission Denied
Good ol' Linux and it's permissions. Simple fix, but simply put your web user does not have access / permission to modify to a directory. Most likely this will be the storage/logs directory within Laravel
sudo chmod -R 775 ./storage
If your error is related to
bootstrap
(i.e. cacheing of service providers and the like), you should run this as well / instead;sudo chmod -R 775 ./bootstrap
Both commands should be run from the root of your project
Summary
These are very basic and common issues that you'll face when using Laravel. Whether that's as a new user, or a seasoned veteran. We all come across these, and there's no shame in forgetting how to fix them sometimes.
Hell, I'll probably open this exact article up next week because I've forgotten to set a csrf
token somewhere, or not generated an Application Key - it happens to us all.
Next post, I'm hoping to put something together surround the Laravel Community, some of it's more prominent figures and what they're known for. If you have any requests; let me know!
O