Advanced features in Laravel 5.4

  • Cubettech
  • Laravel Development
  • 7 years ago
Advanced features in Laravel 5.4

Laravel framework is undoubtedly the most popular PHP frameworks available today that is exclusively designed for development of model view controller (MVC) applications. The Laravel framework comes with some fantastic features inbuilt making it so popular among developers around the globe.

Laravel has launched its latest version Laravel 5.4, which has gained some serious interests among tech enthusiasts. Laravel 5.4 continues the improvements made in Laravel 5.3 by adding in some excellent new optimizations and features such as Like Markdown mail, Dusk, Mix, Real-time facades etc.which we will discuss in larger detail this blog. In addition to the new features being added, the entire code base of the framework is reviewed and restructured making it even cleaner.

Now, let’s talk about the all new features in Laravel 5.4 :

  • Markdown Mail & Notifications
  • Laravel Dusk
  • Laravel Mix
  • Blade Component and Slots
  • Broadcast Model Binding
  • Collection Higher Order Messages
  • Object based Eloquent Events
  • Job Level Retry and Timeout
  • Request Sanitization Middleware
  • “Realtime” Facades
  • Custom Pivot Table Models
  • Improved Redis Cluster Support
  • Migration Default String Length


Markdown Mail & Notifications

Laravel 5.4 comes with pre-built Markdown email templates and mail notifications into your mailables. Markdown templates have the ability to produce beautiful clean HTML mails and the same time it has the ability to make plain text mail.

Laravel Dusk

Laravel dusk is an end-to-end testing tool that allows testing javascript driven interactions such as clicks, focus etc. For example, You can test using dusk whether the user is entered to the correct page after login. Dusk is run along with standalone chromeDriver installation.

Laravel Mix

Laravel mix is firstly introduced in Laravel 5.4, which is a successor of Gulp based Laravel Elixir. It is based on webpack. In Mix, you can build asset pipeline using simple method chaining.

Blade Component and Slots

Blade Component and Slots provides the same usage of sections and layout. For example, consider the following reusable alert message

<div class="alert alert-danger">    {{ $slot }} </div>

The variable $slot contain the content that we wish to inject. We can use @component directive to define the content that is to be injected.

@component('alert')    <strong>Whoops!</strong> Something went wrong! @endcomponent

It also supports named slots which allow  providing multiple slots into a single component.

Broadcast Model Binding

In Laravel 5.4, Broadcast model binding we can request the entire Model instead of requesting numeric order id or something else. For example:

use App\Order; Broadcast::channel('order.{order}', function ($user, Order $order) {    return $user->id === $order->user_id; });

It binds Order model to the channel route. Broadcast Model Binding is very helpful in making realtime applications such as livechat sytems, live boards etc.

Collection Higher Order Messages

Collections in Laravel 5.4 now supports higher order messages. Higher order messages are shortcuts for actions performable on collections. The collection methods that provide higher order messages are: contains, each, every, filter, first, map, partition, reject, sortBy, sortByDesc, and sum.

For example:

$users = User::where(‘group’, ‘Development’)->get();

$users is the collection of users under the group Development. Suppose we need to get the sum of any field say ‘votes’, we can use higher order message ‘sum’ as follows

$votes = $users->sum->votes

Object based Eloquent Events

Now we can use event models as eloquent event handlers. We can define the event handlers in the $events array on your Eloquent model. The following example shows the usage of Eloquent event:

<?php namespace App; use App\Events\UserSaved; use App\Events\UserDeleted; use Illuminate\Notifications\Notifiable; use Illuminate\Foundation\Auth\User as Authenticatable; class User extends Authenticatable {    use Notifiable;    /**     * The event map for the model.     *     * @var array     */    protected $events = [        'saved' => UserSaved::class,        'deleted' => UserDeleted::class,    ]; }

Job Level Retry and Timeout

Instead of configuring retry and timeout globally, Laravel 5.4 allows it to configure them on the Job class itself, so it allows configure retry and timeout per job basis.

<?php namespace App\Jobs; class ProcessPodcast implements ShouldQueue {    /**     * The number of times the job may be attempted.     *     * @var int     */    public $tries = 5;    /**     * The number of seconds the job can run before timing out.     *     * @var int     */    public $timeout = 120; }

Request Sanitization Middleware

It introducing two new middlewares on Laravel’s default middleware stack viz. TrimStrings and ConvertEmptyStringsToNull. As the names indicates TrimStrings will trim all the strings on request and ConvertEmptyStringsToNull will convert all the empty strings to null value.

Realtime Facades

In Laravel 5.4, you can easily convert your application class into a facade. Previously, you could only use built-in facades in your application.

<?php namespace App\Services; class Student {    protected $total;    /**     * Create a new student instance.     *     * @param  Total  $tax     * @return void     */    public function __construct(Total $total)    {        $this->total = $total;    }    /**     * Get the total marks.     *     * @return float     */    public function getTotal()    {       return    $this->total;     } }

We can easily use the class Student as a facade like:

use Facades\ {    App\Services\Student }; Route::get('/marks', function () {    Student::getTotal(); });

Custom Pivot Table Models

In Laravel 5.4 we can define custom models for our pivot tables. By using ‘using’ method we can define the relationship for the custom model.

<?php namespace App; use Illuminate\Database\Eloquent\Model; class Role extends Model {    /**     * The users that belong to the role.     */    public function users()    {        return $this->belongsToMany('App\User')->using('App\UserRole');    } }

Improved Redis Cluster Support

In previous versions of Laravel, clustering of the single host in the same application was not possible. In Laravel 5.4, you can now define Redis connections to multiple single hosts and multiple clusters within the same application.

Migration Default String Length

The default character set for Laravel 5.4 is utf8mb4, which has support for storing emojis in the database. If we choose to switch to this character set manually and are running a version of MySQL older than the 5.7.7 release, it may need to manually configure the default string length generated by migrations within our AppServiceProvider.

use Illuminate\Support\Facades\Schema; /** * Bootstrap any application services. * * @return void */ public function boot() {    Schema::defaultStringLength(191); }

Conclusion

Laravel 5.4 came up with the these latest features which will help ease the developer’s coding effort and helps the client to get the best user experience through Laravel based responsive web solutions. In this blog, I’ve only briefly explained the key features of Laravel 5.4. Will update more about laravel framework in my upcoming blogs.

Cubet Techno Labs is one of the pioneers who understood the benefits of Laravel and adopted it as a core technology stack. We are one of noted Laravel application development company having experience working with some of the largest Laravel based applications.

To add a final note, every new Laravel Development projects at Cubet will be shipped and coded in 5.4 from now.

Talk to our Laravel Professionals

Other Blogs:

Table of Contents

    Contact Us

    Contact

    What's on your mind? Tell us what you're looking for and we'll connect you to the right people.

    Let's discuss your project.

    Phone