27th Feb 2020

Laravel VII : Abbreviated

Web

Written By, Krunalsinh Jadeja

Overview

Hello there. As we all know the Laracon Online 2020, the largest online Laravel conference, took place on 26 February 2020. I happened to attend a Laracon Online Viewing Party and I’m going to go ahead and share with you the highlights. We’re going to focus on Laravel 7 here. Yes, it’s here and stick till the end to know all about it.

So as most of you might know Taylor Otwell was one of the speakers at the event. He gave us a complete walkthrough for Laravel VII and I’m going to cover most of it here.

What is Laravel Airlock?

  • Airlock is a lightweight Token Generation and verification tool (Provides Authentication) mostly for SPAs (Single Page Applications), where users can generate multiple API tokens and like passport, we can set the roles for particular auth token.
  • AirLock will work with Laravel 6.x, but everyone recommends using it with Laravel 7.x and you also need to use Laravel UI 2.x for UI files.
  • We can set allowed domain in config file, so if a request comes from that particular server then and only then that request gets served. So we can say airlock is a similar kind of structure like a passport but it’s for SPA.
  • For better understanding,we can compare AirLock mechanism with Node/Angular project where frontend API will use AuthToken. Authtoken is similar kind of personal access token which we are used in the passport for mobile authentication
  • Key features of AirLock:
    • EncryptCookies
    • AddQueuedCookiesToResponse
    • StartSession
    • VerifyCsrfToken

Laravel Custom Casts

In Laravel VII, we can create our own casting for an eloquent model, there are two methods, “get()” and “set()”

  • “get()” method is used to convert raw data into casted data.
  • “set()” method is used to convert casted data into raw data value.

For example, if we need to decode data when we receive it and encode data when we save details, we can use these methods like this:

Syntax for “get()” method:

Syntax for “set()” method:

For eloquent we need to define detail as:

So now every time we fetch data of a column, we get JSON decoded data and when the data is saved to the column it will get encoded.

HTTP Client

HTTP Client is used for making an HTTP request from one website to another website or web application. For HTTP client you have to install guzzlehttp/guzzle package into your project.
We can make any request along with header through the HTTP Client, also we get the details of response and other params directly, like if we want the main body of the response, then just write down $response->body() this will return the body. If we need to check the status of the response then just need to call $response->status().

Likewise, we can use the following details:

  • $response->body();
  • $response->json();
  • $response->status();
  • $response->ok();
  • $response->successful();
  • $response->serverError();
  • $response->clientError();
  • $response->header($header);
  • $response->headers();

And for the routes we have to write details like:

So now onwards we can fire the API from the web routes along with the Headers. We can also pass the Bearer Token by just adding:

Fluent String Operations

We can all agree how useful string operations are as they help us manipulate strings easily and without much hassle. Thanks to Laravel VII, some new and useful string operations were added in this version. String operations such as trim(), replace(‘x’, ‘y’), after(), before(), words() and many more now will be available in Laravel VII.

CORS Support

Laravel VII also came with the fresh first-party package “CORS” along with options, so now no need to create custom middleware for the same, just configure the config files and use CORS services.

Stub Customization

This is one of the best updates of Laravel. This will help us to boost up the speed of development. For example, whenever we create a new Model, we will write protected $guarded = [*] in all the models, it becomes a bit time consuming if you see it on a larger picture. So now, we can create our own stub for these kinds of changes, So, if we added protected $guarded = [*] into stub then whenever we create a new model, this line will be added automatically. For example, in all tables we need one default column $table->string(‘custom_id’), in all the migrates, so we will publish the stub and customize it.

Route Model Binding

This is one of the most interesting features of Laravel 7. In the older version, we can bind any model to route, for example:

Here we’ll get the user’s personal details like we applied dependency injection of the User model. In Laravel 7, there is support for Implicit Model Binding, such as if I want to get the user’s details based on the user’s name then I’ll get details by adding simple “:” after model’s name.

We can also add custom keys and scoping for the Route Model Binding, it’s a very useful thing which can help us to generate SEO friendly URLs.

Custom Keys and Scope :

    • Sometimes, we need to bind multiple models to the single route and second binding param is dependent on the first building param like we need to get product details that are available under one specific category.
    • For example, we get all the lists of all the mobile devices which are available under the android category, So now we can do this with simple Route Model Binding

    • One more simple example :

Laravel Query Cast

  • Query cast is used to “cast” a column while executing the query.
  • Let’s take an example :
    In the database we save user’s ID, System IP, create_at and updated_at details when user LoggedIn to the system. Now we want to know the last login date of particular user in that case my code will be:

    So here we get the user’s last loggedIn details in “data”, not in the timestamp, we cast the user’s create_at column to date.

Improve email template design

In Laravel 7 they have simply improved the email template, made the email template simpler and finer.

Queue Configuration

Currently, if any queue or job is throwing an exception, each time the job will get executed and throw the exception. In Laravel 7, we can set maxExceptions for jobs. If we set the value equal to 3, in that case, if the job gets the exception more then 3 times, the queue stops automatically. #noExtraExecution

Speed improvements for route-cache

Laravel 7 have 2x speed improvement for routes, it is mostly used with 800+ routes (big projects), php artisan route:cache is used to cache the routes.

Conclusion

So that’s all for this blog but that’s not all for Laravel 7 and our discussion on it. Stick with us for the next blog regarding some of the features along with their demo codes. Till then, get started with the master document. You can read the master document here.

For helping me complete this blog in such a short interval of time, I’d heartily like to thank Kirtan Gajjar, Gordhan Chauhan,Nikunj Bandhiya and Yash Ganatra.

Written By,

Web developer at Yudiz Solutions Pvt. Ltd