in Review, Uncategorized

Top features in Laravel 5.3 you shouldnt be missing out on

Laravel 5.3 was released few months back, and came with a bundle of improvements.

So Whats New?

Echo – WebSockets Support

Laravel Echo makes it super easy to integrate web sockets into your app.

Echo has two parts –

  • Integration with Laravel
  • EchoJS – Client side support

Echo can work with either pusher or plain websockets. You dont have to necessarily use EchoJs on your client side, but it adds quite a bit of sugar coating over the raw apis.
Echo when combined with events can broadcast message, all you have to do is implement ShouldBroadcast interface. It can also be used with notification to send message to any notifable entity as well.

Scout is a full-text solution for eloquent models, it works with elasticsearch and algolia out of the box.

All it takes to make an eloquent model searchable is using the Laravel\Scout\Searchable trait and mapping it to an index by searchableAs() you can also define the fields by which you want search.

Scout allows the use of eloquent’s familiar fluent api to power your search

You’ll need to install scout by composer require laravel/scout, and if you plan to use algolia also install the driver by composer require algolia/algoliasearch-client-php

Mailable

Mailable is the new introduction which replaces the classic closure-style method of sending mail. Artisan can help you generate these classes, it introduces a new directory app/Mail. All of the work of building the email out happens inside the build method, and the details of recipients happen by calling methods on the Mail facade. Any data that needs to be injected into the view can be injected into the constructor of Mailable. No new feature as such but just makes up for a cleaner code.

Mail::to(User::take(3)->get())
    ->cc(User::find(50))
    ->send(new Reminder);

Notification

There are multiple ways to communicate information to a user, be it mail, sms or a app notification. Notification class groups all these representation at one place and gives a convenient api over it.
Notification::send($notifiable, $notification);
Any Eloquent model which uses the trait notifiable can be used to define to whom the notification can go. The notification class decides the channels to be used and its corresponding representation. Again Artisan can help you create these class, by default these are stored in app/Notification. Out of the box laravel comes with few channels like – mail, sms, nexmo, slack and database(just stores notification in db).
Dont find the channel you are interested in here look at Laravel community driven channels

Queues

With laravel 5.3 there are couple of small but convenient changes in how you can interact with a job.

  • With the onConnection() method you can specify which connection to use for the job in the fluent api just like onQueue()
  • New flag retry_after, this defines(in s) the time after which the if the job is still running will be returned to queue again

Thanks for reading this far.

Bonus

Some Miscellaneous features that makes coding in laravel more awesome.

  • Break your routes
    • The concept of web and api middleware was introduced in 5.2, with 5.3 you can split your routes into routes/web.php and routes/api.php
  • Route model binding
    • Instead of getting userId from route and then doing a User::find(userId) directly get the model in route

Write a Comment

Comment