Saturday 28 January 2017

New features in Laravel 5.3.

Hi everybody,

This is after a long time, but having information about new features in Laravel 5.3.

Blade's foreach $loop


This new release introduces a $loop variable inside a foreach loop. This variable provides a couple of properties that can help us easy manage indexes, increments in loops etc. Now in Laravel 5.3, we can do stuff like.

<ul>
    @foreach ($countries as $country)

        @if ($loop->count)
            @if ($loop->first)
                <li class="list-item list-item--header">{{ $country->name }}</li>
            @elseif ($loop->last)
                <li class="list-item list-item--footer">{{ $country->name }}</li>
            @else
                <li class="list-item">{{ $country->name }}</li>
            @endif
        @endif

    @endforeach
</ul>

# Multiple Migration Paths


If you have ever installed a Laravel package that needs a database migration, it involves you copying some migrations into the migration folder, but with Laravel 5.3 — a package can define its migration path, and when you run php artisan migrate all migrations will run without you having to clutter your migration directory.

To let Laravel know about another migration directory, simply do.

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

# Query Builder Returns a Collection


The query builder used to return an array. If we wanted to perform a large array of operations on the returned dataset, we would have to loop through the array. But collections makes that really simple for us to handle. So because of that reason, the query builder just like the ORM now returns a collection of objects. There is a pull request on the topic.

Now we can do stuff like this.

$result = DB::table('posts')->get();

if ($result->first()) { }
if (!$result->isEmpty()) { }
if ($result->count()) { }
if (count($result)) { }

This was not available before. Now, we can do cool operations like pluck, filter etc.

# Migration Rollback in Steps


Instead of rolling back our entire migrations, we can now rollback in steps. With the new step flag added to artisan. So, now we can do this.

php artisan migrate:rollback --step=1

# Ability to Customize Simple Pagination


Laravel is bringing back the ability to customize simple navigation with views. You can also still use the pagination templates if you want, but this is still a nice feature to have. We can do stuff like this.

<ul class="pagination">

    @if ($paginator->onFirstPage())
        <li class="disabled"><span>«</span></li>
    @else
        <li>
            <a href="{{ $paginator->previousPageUrl() }}" rel="prev">«</a>
        </li>
    @endif

</ul>


# Laravel Echo


Echo is meant to be an improvement over the current event broadcasting system. It seamlessly integrates with pusher's JavaScript library.

Taylor Otwell has made a video to better explain Echo. Echo also has an npm package that makes using sockets on the frontend easier.

# Misc and Conclusion


Other features included with version 5.3 are:

    Eloquent Collections are cleanly serialized and re-pulled by queued jobs.
    Queue console output changed to show the actual class names.

    firstOrCreate: now allows additional parameters.

In conclusion, maybe in a major release — support for MongoDB.