New features in Laravel 5 and higher versions

Author - Umang Pasricha
30.07.2018
|
0 Comments
||

The list of some new and useful features in Laravel 5 and higher versions

1. Migrate:fresh Command: Refresh command runs all the down methods from database migrations. There may be some cases where down method for some database migrations are not given which can create problem. For this reason in Laravel 5.5, they introduce new command – migrate:fresh. This command rollbacks by dropping all the tables. Then run again all the up methods from migration files. This command is more useful, secure and there is no need to define down methods separately.

2. Made vendor:publish command more explicit: In earlier versions, this command published all resources from packages, framework and in some cases migrations, views, and configs too. But now in Laravel 5, this command is made explicit, now this command allows us to view only the things we need. This command asks us to choose a flag.
Here are some options:

		$ php artisan vendor:publish
		Which provider or tag’s files would you like to publish?
		[0] Publish files from all the providers and tags listed below.
[1] Provider: Illuminate/Notifications/NotificationServiceProvider
[2] Provider: Illuminate/Mail/MailServiceProvider
[3] Provider: Illuminate/Pagination/PaginationServiceProvider
[4] Tag: laravel-notifications
[5] Tag: laravel-pagination
[6] Tag: config
[7] Tag: laravel-mail
>

You can choose from above options. This will give us more specific information. Also we can skip this prompt by directly passing flag in the command.
Eg,

	$ php artisan vendor:publish --all
			Or
	$ php artisan vendor:publish --provider

This will publish all the files from above list.

3. Real-Time Facades: Previously, we could expose facades and provide quick access to laravel methods using laravel’s own services via the service container. But now, you can convert any of your application’s classes into a facade in real time simply by prefixing the imported class name with Facades.

Example – Let us assume we have class with name ‘Payment’. We can now use this class as facade like-

	use Facades\ {
    		App\Services\Payment
	};

	Route::get('/pay/{total}', function ($total) {
    		Payment::pay($total);
	});

4. Introduced Higher Order Messages for collections: These higher order messages provide shortcuts for some common actions made on collections. Here is the list of some methods:
Contains
Each
Every
Filter
First
Map
Partition
Reject
sortBy
sortByDesc
Sum

Example:

$users = Order::where('date', '2018-02-08')->get();
	return $users->sum->amount;

This will give us total order one gets on the given date.

5. Migration Default String Length: Laravel 5.4 uses utf8mb4 character set by default, which includes support for storing “emojis” in the database. If you are upgrading your application from Laravel 5.3, you are not required to switch to this character set.
If you choose to switch to this character set manually and are running a version of MySQL older than the 5.7.7 release, you may need to manually configure the default string length generated by migrations. You may configure this by calling the Schema::defaultStringLength method within your AppServiceProvider:

use Illuminate\Support\Facades\Schema;
	public function boot()
	{
    		Schema::defaultStringLength(191);
	}

Also, if you get this error-”Specified key was too long error”, you can add this default string length code to your AppServiceProvider. This will resolve your issue.

6. Storing Uploaded Files: Laravel 5.3 provided a way to store user’s uploaded files such as photos or documents using a new store method on an uploaded file instance.
Example:

$path = $request->file('myfile')->store('/uploaded files');

This will store your file ‘myfile’ in the uploaded files folder with the same name. If you want to change the name of file use ‘storeAs’ method.

7. Routes Files: Laravel 5.3 provided two HTTP route files- web and api in a new top-level routes directory. This splits the routes for your web interface and your API. The routes in the api route file are automatically assigned the api prefix by the RouteServiceProvider.

8. $loop Variable: This variable is used inside the loop within blade template to get some useful information regarding current loop like current loop index, first/last iteration of loop.
Example:

		@foreach ($orders as $order)
    			@if ($loop->first)
        				First order.
    			@endif
		@endforeach

9. New Blade directives: Laravel 5.5 provides two directives- @auth and @guest instead of @if (auth()->check()).

10. Route Helpers: You can use Route::view() and Route::redirect() helpers within your routes.php file for loading a view and performing a redirect respectively directly from route file.

11. Collection Dumping: Along with dump() method now we are provided with dd() method ie. dump and die. dump() outputs the results and continue processing while dd() stops the process immediately and dumps out the results. So there is no need to command the code after dumping code while debugging. You just have to use dd() if you want to stop further processing and dumpt current data.

12. Custom Blade “If” Directives: we can create our own blade directives. This will decrease the code complexity of blade files.
Example: for conditionally load html only for the subscribers of your site. We need to do-

	@subscribed
    		Html Code
	@else
    		Html Code
	@endsubscribed

	Rather than this-
	@if (auth()->check() && auth()->user()->isSubscribed())
    		Html Code
	@else
    		Html Code
	@endif

To make custom directive write the following code in the boot method of the AppServiceProvider class:

	public function boot()
   	 {
        		Blade::if('subscribed', function () {
            		return auth()->check() && auth()->user()->isSubscribed();
        	}); 

Webner Solutions is a Software Development company focused on developing Insurance Agency Management Systems, Learning Management Systems and Salesforce apps. Contact us at dev@webners.com for your Insurance, eLearning and Salesforce applications.

Leave a Reply

Your email address will not be published. Required fields are marked *