Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Notes on working with Laravel 5

Working with Laravel 5



Oh My Zsh

Within ~/.zshrc:

plugins=(git laravel5)


Show version: php artisan --version

Show all commands: php artisan list

Show all routes: php artisan route:list


Key: php artisan key:generate

Secure Dotfiles on Apache

<FilesMatch "^\.">
    Order allow,deny
    Deny from all

Setup Homestead with Vagrant

Multiple PHP versions

Execute a command in the command line with a particular PHP version:

  • php5.6 artisan list
  • php7.0 artisan list
  • php7.1 artisan list

Fix authentication failure on bootup

Problem: $ vagrant up results in authentication failure.

default: Warning: Authentication failure. Retrying...

Solution: SSH into the VM via $ ssh vagrant@localhost -p 2222 and remove and regenerate the private keys:

wget -O .ssh/authorized_keys
chmod 700 .ssh
chmod 600 .ssh/authorized_keys
chown -R vagrant:vagrant .ssh


Install Mcrypt via Homebrew: brew install mcrypt

Multiple PHP versions

Install Mcrypt for a particular PHP Version: sudo apt install php5.6-mcrypt

Setup Permissions

Make sure to grant Apache access to store data within storage:

  • storage/app
  • storage/framework
  • storage/logs



Daily logs: Add APP_LOG=daily to your .env config file (see config/app.php).


Populate: php artisan db:seed

Populate a specific database: php artisan db:seed --database mysql_testing


Create new migration: php artisan make:migration create_tasks_table --create=tasks » see database/migrations

Migrate: php artisan migrate

Migrate, again: php artisan migrate:refresh

Migrate a specific database: php artisan migrate --database mysql_testing


Create new model: php artisan make:model Task » see app

Create new model and do migration: php artisan make:model Task -m


Create new policy: php artisan make:policy TaskPolicy » see app/Policies


Create views: php artisan make:auth --views » see resources/views/auth


Create: php artisan make:middleware BeforeMiddleware » see app/Http/Middleware


Note: The name of the event is usally set in past tense (event is already done).

Add new event with listeners in app/Providers/EventServiceProvider.php

Create new events automatically based on your EventServiceProvider: php artisan event:generate » see app/Events and also app/Listeners

Artisan Console

Create command: php artisan make:console AppAbout --command=app:about » see app/Console/Commands

Register new command: » see app/Console/Kernel.php

Usage: php artisan app:about


Usage: php artisan make:test UserTest


Add testing database credentials to your phpunit.xml file:

<env name="DB_HOST" value=""/>
<env name="DB_DATABASE" value="homestead"/>
<env name="DB_USERNAME" value="homestead"/>
<env name="DB_PASSWORD" value="secret"/>

Use temporary in memory database:

<env name="DB_CONNECTION" value="sqlite"/>
<env name="DB_DATABASE" value=":memory:"/>


Re-generate autoload: composer dump-autoload

List dependencies of all dependencies: composer show --tree

Route Caching

Cache: php artisan route:cache

Clear: php artisan route:clear

Interacting with the application

Usage: php artisan tinker

E.g. create User: $user = factory(App\User::class)->create();

Performance & Caching

Caching for production

Composer: composer dump-autoload --optimize

Use controllers instead of closures within routes/web.php file to cache routes. Use route:cache to skip route compilation on each request:

php artisan route:cache

Use config:cache to reduce configuration parsing:

php artisan config:cache

The config cache includes config files and also the .env file.

Attention: Make sure to not use env() anywhere in the code base beside the config files! env() will return null with config:cache. Use config() helper function instead.

Generated cache files can be found in: /bootstrap/cache

Eager loading

Use eager loading to avoid the n+1 query problem. Save database queries using the with() helper function and reduce the amout of queries made in total.


Database chunking

Reduce server memory usage on queries returning lots of data to not overload the server. Attention: This may increase the database queries and overall loading time! Use chunk() helper function in the query builder.

MySQL indexing

Add index (KEY) columns based an where statemants whenever possible as MySQL is quite fast at indexing which will result in faster queries.

MySQL explain

Set the debug bar config to explain within /config/debugbar.php to explain each SELECT statement:

'explain' => [
  'enabled' => true

Multi-column indexes

Set multi-column indexes as new migration from database/migrations/add_table_name_indexes.php:

Schema::table('table_name', function(Blueprint $table) {
  $table->index(['user_id', 'created_at']);

Object caching

Use a key-value storage option like Redis or Memcached. Set Redis drivers within .env file:


And require predis via composer: composer require predis/predis

Make sure to keep an eye on Redis memory usage on user heavy applications and save as litte information as posssbile to Redis to reduce memory usage.

Cache usage

Use and combine auth()->user()->id and other dynamic parameters to generate a unique cache key with md5(). Use the decorator pattern when adding caching to the app.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment