Tag Archives forphp

Laravel migration and renaming a column

For a Laravel project I created a migration in which I added several attributes to a database table and also renamed an existing attribute. I used the following code in the up() method of the migration:

Schema::table('table', function (Blueprint $table) {
    $table->renameColumn('existing_column', 'renamed_column');

I ran the migration for my Sqlite test database, but after running my tests it turned out the rename was not executed. However, the new attribute was added to the table.

After some experimenting I found out that you should do column renames in separate calls to the Schema facade. After changing the code in the up() method of my migration to the following everything worked as expected:

Schema::table('table', function (Blueprint $table) {

Schema::table('table', function (Blueprint $table) {
    $table->renameColumn('existing_column', 'renamed_column');

I don’t know if this issue is specific to Sqlite or that it also occurs on other databases such as MySQL or PostreSQL. I also have not spent time looking into the cause of this issue. If you know or have an idea please let me know in the comments 🙂

January 4, 2016

Get the name of the current view in Laravel

To fix a problem in a Laravel application I needed access to the name of the current view in the master layout. I found a nice solution for this problem in this post on StackOverflow:

View::composer('*', function($view){
    View::share('view_name', $view->getName());

I added this code to my AppServiceProvider, and I can now use the $view_name variable in all my views. It will contain the name of the view as you called it from your controller, for example pages.home.

One thing to note is that there is a comment to this solution on StackOverflow that says that this works, but that the $view_name will be overwritten if you use @include for partials in your views. In my case this is not an issue, but something to keep in mind if you run into problems.

December 16, 2015

Spatie Laravel Backup and Envoyer

laravel-logoTo setup automated backups for a Laravel application I decided to try out the Spatie Laravel Backup package. It can backup your application to all Laravel filesystems you have configured.

With this package you can configure which files you want to include and exclude in your backups. The default configuration of this package includes the base_path() directory and excludes the storage_path() and base_path('vendor') directories.

However, I have some files under the storage_path() directory that I do want to backup, so I modified my configuration to include those files. When I ran the backup locally everything worked fine, but when I tested it out on my production server the files in the storage_path() directory were missing.

After some digging around I found out that this came from the fact that I use Envoyer to deploy my application. Envoyer stores the last four releases and uses a symlink to point to the current release. It also uses symlinks for files/directories that should be shared between releases, for example the .env file and the storage_path() directory. The directory structure used by Envoyer looks something like this:

    current -> /path/to/application/releases/20151209090624/
            .env -> /path/to/application/.env
            storage -> /path/to/application/storage/

If you configure your backups to include the base_path() directory with the directory layout above, the files in the storage directory will not be included. This is because the call to base_path() will output the real path to the current release, which will be /path/to/application/releases/20151209090624/ for the example above. However, the storage_path() directory is not included in this path, since it is located at /path/to/application/storage/. That is why the files in the storage_path() directory were not included in my backups.

The fix for this problem is easy though, you can simply add the storage_path() directory to the list of included directories you configure for you backups and they will be included in your backups.

December 9, 2015

Laravel absolute URLs from background queue

laravel-logoFor a project I use background queues to send confirmation emails. In the Blade template of this email I use the link_to_route() helper function. But when sending the emails through the background queue the domain for this link defaulted to localhost.

Normally Laravel falls back to the domain of the request, but that is not available when code runs in the background queue. I fixed this by adding the following line of code to the register() method of my AppServiceProvider:


In my case I also added a APP_URL environment variable, because I want to set the correct URL for all environments of the app, and chaged the code to the following:

December 7, 2015