With the 2016 Laracon US conference already sold out and just but a few weeks away, it is expected that Laravel 5.3 will be released during the 3 day conference from July 27th to July 29th of 2016.
We’ll have Chris, Nick, and Mathias out at Laracon this year so look for a first-hand recap during Laracon.
In this article, we will take a look at what to expect in 5.3
In May 2016, Taylor Otwell made a screencast on Laracasts introducing Laravel Echo using Laravel Spark as one of the new things coming in Laravel 5.3.
Laravel Echo is a vast improvement over the event broadcasting system In Laravel and how it Interacts with Pusher.
-Taylor Otwell
Laravel Echo basically makes it easier to work with web sockets by removing all the complexities involved in making real time web applications intuitive.
There is no documentation yet on Laravel Echo so we will not delve into how to create a real time app sample just yet.
For those of you that dare venture, such as Matt Stauffer that have gone ahead to dig into the source, you may tinker with Laravel Echo in one of the following ways
$loop
VariableLaravel’s blade templating engine provides convenient directives for working with PHP’s control and loop structures.
Taking a look at the loop structures, Laravel allows us to iterate through data with ease.
<ul>
@for($i = 0; $i < 10; $i++)
<li>{{ $i }}</li>
@endfor
</ul>
@foreach($users as $user)
<p>{{ $user->name }}</p>
@endforeach
Laravel 5.3 will be introducing a new $loop
variable that will be available for each iteration in the @foreach
directive.
The loop variable will have some of the following properties.
first
: Boolean value that evaluates to true on the first iteration.last
: Boolean value that evaluates to true on the last iteration.index
: A 1-based index of the items in the loop with the first item being at index 1.count
: The number of items on the loop.remaining
: The number of items remaining in the loop. For n items in a loop with the current position being 4, this value would be n-4
depth
: Integer value of the depth of the loop.parent
: Returns a reference to the parent $loop
for a foreach
with a depth of more than 1, otherwise, null for a loop with a depth of 1.What this means is that you will be able to do cool things like:
@foreach($leaderboard as $player)
<p>{{ $player->name }} - {{ $loop->index }} of {{ $loop->count }}</p>
@endforeach
@foreach($leaderboard as $player)
@if($loop->first)
<p class="winner">{{ player->name }}</p>
@continue
endif
@if($loop->last)
<p class="loser">{{ player->name }}</p>
@continue
endif
<p>{{ $player->name }}</p>
@endforeach
While returning data from a database model returns JSON data, this has merely been an array that can be converted to and from JSON format.
In Laravel 5.3, you can now look forward to a new way of querying and updating data based on the JSON properties.
In this section, we will use the same example of a players table.
A player can have attributes that are vital to the player character, this details which include name, date of birth, phone number and email address are justified to have their own respective fields.
We can however have some additional data that may not be key to identify the character of a player record but may be necesarry to have as part of a player’s profile.
Our Player migration will look like this:
Schema::create('players', function (Blueprint $table) {
$table->increments('id');
$table->string('name');
$table->date('dob');
$table->string('phone_number');
$table->string('email');
$table->json('stats');
$table->timestamps();
});
This means that with a record template that looks like this, we can query and update the data based on the statistics of each player using the arrow notation.
{
"id": 1,
"name": "John Kariuki",
"dob": "2016-06-07",
"phone_number": "XXXXXXXXXX",
"email": "[email protected]",
"stats": {
"goals": 20,
"red_cards": 2,
"yellow_cards": 3,
"benched": false
}
}
To get a list of all players with 20 goals, we will make a query whose where clause has two parameters. The first being the json field name with an arrow notation followed by the property name, and the second being the value to equate to.
DB::table('players')
->where('stats->goals', 20)
->get();
We can also update the player details based on a value in the JSON field.
DB::table('players')
->where('stats->red_cars', '>', 3)
->update(['stats->benched' => true]);
As for Laravel 5.3, this feature is limited to databases that have JSON data type support such MySQL 5.7 and PostgreSQL 9.5.
Take a look at how Prosper Otemuyiwa, an open source envagelist, plans on taking advantage of the JSON column to index and look up data on this Facebook post.
The firstOrCreate
method will allow you to provide additional parameters to it.
So instead of having to check if a record exists and creating it like this:
$site = Site::firstOrNew(['user_name', 'scotch']);
if (! $site->exists) {
$site->fill(['website' => 'https://scotch.io'])->save();
}
return $site;
You can now do this:
return Site::firstOrCreate(
['user_name', 'scotch'],
['website' => 'https://scotch.io']
);
In both cases, we will get a site whose name is scotch if it exists but if does not, we will create one with a username of scotch and website value of https://scotch.io.
While the where method in Laravel 5.2 allows you to filter a collection by equality of a given key/value pair, we can expect that Laravel 5.3 to have some additional operators.
$players = collect([
['name' => 'John', 'age' => 20],
['name' => 'Jane', 'age' => 21],
['name' => 'June', 'age' => 22],
['name' => 'Alex', 'age' => 23],
['name' => 'Jude', 'age' => 24],
['name' => 'Luke', 'age' => '24'],
]);
$players->where('age', 23);
///['name' => 'Alex', 'age' => 23]
$players->where('age', '==', 24);
/*
['name' => 'Jude', 'age' => 24],
['name' => 'Luke', 'age' => '24']
*/
$players->where('age', '===', 24);
//['name' => 'Jude', 'age' => 24]
$players->where('age', '<=', 22);
/*
['name' => 'John', 'age' => 20],
['name' => 'Jane', 'age' => 21],
['name' => 'June', 'age' => 22]
*/
With the operator parameter, you have the option of using one of the following:
= == === != !== <= >= < > <>
This in turn means that the whereloose method will be deprecated since there will be no need for loose comparisons.
In Laravel 5.2, the database query builder returns an array where each result is an instance of the PHP StdClass
object.
This is bound to change in Laravel 5.3 to return a collection instead.
I personally think will be a really good change since you do not need the collect
method to convert the array to a Laravel collection. It also goes without saying that collections are much easier to work with as opposed to arrays.
Laravel 5.3 will support multiple migrations paths through a service provider.
$this->loadMigrationsFrom('path/to/your/migrations/folder')
With this, running migrations from your Laravel project and all the packages that need to run migrations is as easy as php artisan migrate
While this may not seem as a big improvement compared to some of the other improvements, migration paths are bound to make it easier to load migrations from packages.
According to Alex Bowers in this article,
A required step previously was to have a publication step, or a copy paste step for the migrations from vendor/package/database/migrations to database/migrations.
This new feature allows the migrations to stay where they are, and not clutter up your migrations folder.
Laravel 5.3 is set to provide a new option to enable you to rollback one migration as opposed to the entire migration
php artisan migrate:rollback --step=1
Laravel makes it all too easy to store uploaded files to any of your disks as configured in config/filesystems.php
. In this case, disks refer to your storage location which currently include:
Read more about the specific configuration for each storage drive on the filesystems documentation.
In Laravel 5.3, the UploadedFile
class has a store
method that accepts the file path to store the file and an optional second parameter of the storage location of choice.
$storagePath = $request->avatar->store('images');
$storagePath = $request->avatar->store('images', 's3');
The file name is intentionally omitted since an md5 hash of the file contents is generated as the file name.
To override this implementation and store a file with a name of your choice, use the storeAs
method which accepts the file name as a second parameter
$storagePath = $request->avatar->storeAs('images', 'avatar.jpg');
$storagePath = $request->avatar->storeAs('images', 'avatar.jpg', 's3');
Taylor Otwell recently tweeted about Documentation driven development
and if you follow him, well then let’s just say you will always be up to date on peek previews of what’s coming.
He wrote a tweet on June 1st 2016 on customizing pagination using views which was in response to this issue on Github. This makes it easier to have custom pagination styles as opposed to being limited to the Bootstrap template implementation.
Some improvements in the Laravel queue service have been mentioned on twitter by Taylor Otwell and @laravelphp.
I have just but lightly touched on what to look out for on Laravel 5.3 which is expected to support PHP 5.6, PHP 7 and HHVM.
While I have not touched on the methods that are being done away with besides whereloose()
, I would like to mention the lists()
method which will be removed in favour of the pluck() method.
Some changes are also expected in the Validation class. What changes have you seen in Laravel 5.3 so far? What are you looking forward to in 5.3? Let’s get talking on the comment section.
☞ How to use Advanced Laravel Eloquent
☞ Laravel 6 PHP Framework Tutorial - Full Course for Absolute Beginners