How to Laravel series: What is new in Laravel 4.1
#laravelSo big news today. Taylor Otwell just released Laravel 4.1! We all have been looking forward to this new version and I can tell you it was worth the little delay. Additionally the official Laravel site got a big update and a new look. These are really good news and I will give you a short overview of what is new in Laravel 4.1 in this article.
Lightning installation
Since 4.1 there is a brand new Laravel installer which makes the installation extremely fast and simple.
It is an phar archive you can download from the official Laravel documentation. Afterwards you put it in the /usr/local/bin directory, so you can use it globally. You probably have put there your composer.phar file too, because of the same reason. It is recommended to rename the Laravel installer to laravel, but this is not necessary. In most cases it is required to change the files permissions.
// Move the downloaded installer and rename it
mv /Users/christophrumpel/Downloads/laravel.phar /usr/local/bin/laravel
// Change the premissions
chmod 777 laravel
After these little steps, you can make use of the installer with the following command. It will create a fresh Laravel installation in you preferred directory. (in this case the blog director<)
// Create a new Laravel installation
laravel new blog
Remote component
With the brand new Laravel remote component it is possible to trigger remote commands. One scenario would be to deploy your application. You connect to your server, pull latest updates and maybe you want the cache cleared too. All of this can now be set up easily and run with an artisan command on your local command line. I love this!
Rocketeer is a package which takes advantage of the new Laravel component in order to set up your project and deploy it even faster and easier.
Polymorphic Many-to-Many relationships
Sometimes it is useful to be able to attach a many-to-many relationship not only to one model. Let’s say you have posts and pages on your sites. Both can have many tags. On the other site tags are related to many pages and posts. In this case we speak of polymorphic many-to-many relationships. There have been many requests for this feature and voila, in 4.1 it is included.
New Eloquent methods
New Eloquent methods have been added to Laravel 4.1. ‘whereHas’ and ‘orWhereHas’ allow now extra constraints on ‘has’ type queries. In the following example from Mark we are retriecing all posts that have comments from the user with the id 1.
Post::whereHas('comment', function($q) { $q->where('user_id', 1); }, '>', 0)->get();
In Laravel 4.0 we were only able to catch all posts with comments. These queries are now much more powerful.
Welcome Boris
Boris is an upgrade for Artisan’s tinker command. So what is tinker? This is what I was asking myself too. So I checked it out. Tinker is an REPL (‘Read-eval-print-loop’) and belongs to the Artisan commands. It’s kind of a playground for your application and it runs in a shell. If you know Ruby, then you can think of it as the IRb in Laravel. You can interact with your application and test it with live feedback. Here is a simple example for your better understanding. (I am using the alias "art" so I do not need to write "php artisan" every time) First I start tinker and I set a variable. Afterwards I am counting my users. I am interacting with my application and I could create or delete users too. In order to see a response I need to echo it out though. As I mentioned before, Boris is an upgrade to tinker. Tinker was nice, but it was missing the little tweaks that make it really handy. For example you couldn’t view your last commands through the arrow keys, which I am using a lot. Additionally you had to echo everything out, which gets annoying. Now with Boris we have much more features like a nicer and easier output and better feedback. You will see that we immediately get a response to what we do now.
These are just some simple examples. There is so much more you can do, make sure to check it out yourself. I need to mention that Boris requires the PHP extensions ‘readline’ and ‘posix’. I updated my Mac’s PHP to a newer version, this is why I had these extension already included. You probably need to install them. All about Boris you will find here.
Tail command
With the tail command it is now possible to watch for errors in your console. So where could this be helpful? Let’s say your application is not in the debug mode anymore and you do want to look for errors. Yeah you could just look into the log file, but with the tail command there is now a new and easier way. Just type the command and you will see all errors live in the console. This would be a local approach, but it is also possible to watch for remote errors from you local console. This can now be done with the remote component which we mentioned already before. You will have to define a remote connection and then you can run the tail command with the connection’s name as an option. Awesome right?
// Start tail with the production connection
php artisan tail production
But there is more to the tail command. You can use it to show your queries too. I know there are often situations where you need to take a look at your queries. You can print them out, but that’s not ideal. A better way would be to log the queries, because now they will appear when the tail command is run.
Password reminder improvements
There is now a new password reminder controller generator, which will create boilerplate code for you to start. The new controller includes the main methods you will probably need to work with password reminders. There is also a single route which handles all the actions from the created controller.
php artisan auth:reminders-controller
// Singe line to create all your needed routes
Route::controller(‘password’, ‘RemindersController’);
Conclusion
So, these are the main new features in the new version of Laravel. I hope you’re as excited as I am to take advantage of them in upcoming projects. Taylor Otwell and the Laravel community has really done a great job in making this PHP framework even more better. Again!
In fact there are many more improvements. If you’re interested you can check out the list of all changes in your Laravel version with simple running this artisan command:
php artisan changes