Return data from laravel events to controllers

Laravel events are lovely to work with, it makes your controllers much cleaner and it groups multiple tasks. For example, when creating a new user. I used it a lot but I never had to return data back to my controller.

At first, I was a bit worried, I had never seen an example where there was actually data returned to the controller.

But Laravel shouldn’t be Laravel if it was not possible, after a bit of research it seemed that you could just return whatever you want in every listener (in the handle method) of your event. In your controller, you would get an array with a key for each listener you have for the fired event.

# response of the fired event. This event had one listner 

0 => "{"slug":"my-slug","id":1634,"name":"my name","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"XXX","id":123,"name":"Jorenvh","description":"Joren Van Hocht","public":false,"type":"NORMAL","links":{"self":[{"href":"http://github.com/jorenvh"}]}},"public":false,"links":{"clone":[{"href":"http://github.com/jorenvh/blogify.git","name":"http"},{"href":"ssh://git@github.com/jorenvh/blogify.git","name":"ssh"}],"self":[{"href":"http://github.com/jorenvh/blogify"}]}}"]

Each of these keys contain the JSON encoded data that you returned in the handle method of your listener.

Happy birthday Laravel

Five years ago today Taylor announced the first version of Laravel, Happy birthday Laravel.

I’m using Laravel for almost 2 years now and started my journey at version 4.2. Two versions later today the framework has changed a lot and I became a big enthusiast of it.

Many thanks to Taylor Otwell for making my life as a developer more enjoyable.

Automate Composer self-updates

At These Days, the company I work for, we deploy our projects to our dev & staging servers through Slack. We developed a application that takes api calls from Slack slash commands to deploy these projects.

So we don’t ssh manually that much anymore to these servers. It speeds up our workflow but we noticed that we didn’t ran composer self-update that often anymore.

A quick tip so you don’t have to worry about it anymore is to setup a cronjob, we run it every morning at 6 o’clock, that runs this command for us. So no need to worry about that anymore.

Scotch.io published a tutorial a few days ago where in they explain how you can do this for your servers. So I suggested you to check that post.

I’m also writing a more in depth post about our Slack deploy tool so be sure to come back to check out how and why we build this tool. I also gave a talk about our workflow at the March PHP Antwerp meetup.

Export php variables to javascript

Spatie, known for their Laravel packages, released a brand new package. Laravel blade javascript is a Blade directive to export PHP variables to Javascript.

But instead of exporting variables in the controller our package does it in the view.

Read the full article on Freek Van der Herten his blog. You can also find all the other Spatie packages on their website.

New migration and blade features coming to Laravel 5.3

Taylor Otwell announced two new features coming to Laravel 5.3 on twitter today.  You will be able to roll back one migration at a time and you will have access to the $loop variable in blade foreach loops.

Rolling back one migration at a time

This new feature will come in handy when one of your migrations fail for some reason. You will be able to use it this way:

php artisan migrate:rollback --step=1

Blade $loop variable

In blade foreach loops you will have access to a $loop variable, it will allow you to do things like this, but knowing Taylor there will be a lot more available.

@foreach($users as $user)
    @if($loop->first)
        <!-- First iterartion -->
    @endif
    
    @if($loop->last)
        <!-- Last iteration -->
    @endif
@endforeach

Code credits go to Taylor Otwell & Laravel php

Why you shouldn’t set global variables in Base Controller

Laravel Daily posted a new tip today, they explain why you shouldn’t set global variables in Base Controller.

Setting global variables in the base controller works, until you need to add a error page or 404 page. When rendering those pages no controller will ever be hit. So your global variables are not available at that moment.

For the workaround I suggest you take a look at the original article on the Laravel Daily site.

Clean code

Joeri Timmermans, a Symfony developer at Intracto, created a blog post to share some of his personal experiences and tips to write clean code.

Always code as if the person who ends up maintaining your code is a violent psychopath who knows where you live. 

He gives tips about writing cleaner code but also explains why it is so important to write clean code.

You can read the full article on the Intracto blog

Laravel 5.3 – Multiple Migration Paths

A awesome new feature coming to Laravel 5.3 is the ability to have multiple migration paths.

This feature comes in handy for all package developers and of course for all package users. Currently when a package wants to use migrations you have to use php artisan migrate to run your own migrations and php artisan migrate –provider”Package\ServiceProvider” to migrate package migration files.

Once Laravel 5.3 has been released you will be able to register multiple migrations paths like this:

$this->loadMigrationsFrom('path/to/migrations/folder');

from in a service provider for example. The end user can now migrate all the migrations while only using php artisan migrate. There is no need to look all the providers up to migrate package migrations.

Passing variables by Value vs. by Reference Visual Explanation

Penjee released a in depth tutorial that explains the difference between passing by value and by reference.

When writing software code, you will spend a lot of time defining, reading and changing variables. Using a variable means you use a descriptive word in your code which holds some information (a number, some text, an object, etc.). This descriptive word is the “title” of the stored information

Create a global .gitignore file

If you are like me you are probably creating a .gitignore file over and over again for every single project. 90% of these .gitignore files are the same, it’s a waste of time creating these files over and over again.

Recently I discovered you can create a global .gitignore file in your .gitconfig file. You can do that by running this command from your terminal:

git config --global core.excludesfile '~/.gitignore_global'

I named my file .gitignore_global just for clarity but you can name it whatever you want.

You can view my personal global .gitignore file on Github.