What's New in Laravel 6.0?

What's New in Laravel 6.0?

Learn about the new features and products in the recent Laravel 6.0 release.

Originally published by Holly Lloyd at https://auth0.com

Laravel 6.0 is here!

Taylor Otwell, the creator of Laravel, has been teasing the 6.0 release for a few months now and today it has finally officially dropped. Let's take a look at what's changing and how you can start using it right away.

I plan to show some of the best goodies from Laravel 6.0 in my talk at @LaraconEU. Is there anything else you would like to see me cover on stage? 📣
— Taylor Otwell 🏝 (@taylorotwell) August 26, 2019
What's new?

Semantic versioning

Although the jump from 5.8 to 6.0 is a major release, Otwell has mentioned that in terms of new features and changes, this update will feel more like moving from 5.8 to 5.9.

Laravel has had huge updates with every major version release, which is typical in a young framework. All the updates throughout 5.x laid the groundwork for a stable and robust framework. Going forward with 6.0, you won't have to worry as much about huge breaking changes and rewrites with every new major release. Because of this new stability, the Laravel framework package is officially adopting the more traditional semantic versioning.

Job middleware

Instead of manually adding extra boilerplate code around your jobs, you can now extract it into middleware and then just attach it to the job!

This example shows how you could add rate limiting to a job prior to 6.0. You would add it to the job's handle() method for each job that requires it.

// app/Jobs/JobToBeThrottled.php

// Execute the job

public function handle() {

  // allow one job to process every 5 seconds

  Redis::throttle('key')->block(0)->allow(1)->every(5)->then(function () {

    // Handle the job

  }, function () {

    // Could not obtain lock

    return $this->release(5);

  });

}

With this new release, we can extract that code out of handle() and move it into a job-specific middleware. You can create a directory in app/Jobs/Middleware (or wherever you want, Laravel isn't opinionated about this) and add a file for the middleware in that folder.

// app/Jobs/Middleware/RateLimited.php
...
namespace App\Jobs\Middleware;
use Illuminate\Support\Facades\Redis;
 
class RateLimited {
  // Process the queued job.
  public function handle($job, $next) {
    Redis::throttle('key')
      ->block(0)->allow(1)->every(5)
      ->then(function () use ($job, $next) {
        // Lock obtained
        $next($job);
      }, function () use ($job) {
        // Could not obtain lock
        $job->release(5);
      });
  }
}

Now you can attach this middleware to the job and it will run through the middleware before processing. If you're familiar with route middleware, this process is very similar.

// app/Jobs/JobToBeThrottled.php
use App\Jobs\Middleware\RateLimited;

// get the middleware that the job will use
public function middleware() {
 return [new RateLimited];
}

public function handle() {
 // handle the job
}

This will keep your job files small, easier to read, and focused on their specific task. It will also allow you to easily reuse middleware throughout jobs.

Lazy collections

Laravel 6.0 introduces lazy collections, which will allow you to keep memory usage low when dealing with heavy data.

If you're working with eloquent models, you can choose to load one model into memory at a time instead of everything at once by using the cursor() method instead of all().

// This loads all eloquent models into memory at the same time
// This may be a very large number if you have thousands of posts
 
$posts = App\Post::all()->filter(function ($post) {
    return $post->id > 500;
});

By switching from all() to cursor(), only one eloquent model is loaded into memory at a time because cursor() uses the new LazyCollection instance.

$posts = App\Post::cursor()->filter(function ($post) {
  return $post->id > 500;
});
 
foreach ($posts as $post) {
  echo $post->id;
}

Eloquent subquery enhancements

Eloquent has always made it easier to run complex queries. This new release gives you even more help when it comes to running subqueries, or a query within a query, in a single call. This is useful when you need to select information from two tables with a relationship. In Laravel 5.x you were sometimes limited with what you could do in a subquery and would often end up being forced to chain a query using DB::raw().

The addSelect method has been added to subqueries, which should eliminate a lot of that pain! Eloquent subqueries will also now have access to orderBy.

For this example, imagine you have 2 tables: hotels and reservations. You want to know the most recent room type that was booked for a specific hotel. Instead of doing two separate eloquent queries, you can now do this:

use App\Reservation;
use App\Hotel;
 
return Hotel::addSelect(['last_booked_room' => Reservation::select('room_type')
  ->whereColumn('hotel_id', 'hotels.id')
  ->orderBy('created_at', 'desc')
  ->latest()
  ->limit(1) 
])->get();

Improved custom authorization responses

If you're using Laravel's out-of-the-box authentication with gates for authorization, Laravel 6.0 introduces a new method Gate::inspect. This makes it easier to provide custom messages to users during authorization requests, such as a specific error message if their request was denied.

For example, say you have the method below that determines if a user is authorized to edit a post. Before 6.0, it was difficult to retrieve this message and display it back to the user.

With the introduction of the Gate::inspect() method, you can now easily retrieve the full custom response to send back to the user.

If you want to limit the editing of posts to only users who have the role admin, you may have created a gate definition for editing posts.

// App/Providers/AuthServiceProvider.php
...

public function boot()
{
   $this->registerPolicies();

   // Define the gate that determines who can edit posts
   Gate::define('edit', function ($user) {
       return $user->isAdmin
         ? Response::allow()
         : Response::deny('You must be an administrator to edit posts.');
   });
}

Normally you would just use the allows or denies method to see if that user should be authorized to perform an action. The response depends on the boolean response from the Gate::define method defined above. The following example will not give you the custom response, just the boolean.

if (Gate::allows('edit')) {
 // let the user edit
}

if (Gate::denies('edit')) {
 // let the user edit
}

If you want to access the more detailed custom denial response, you can now use Gate::inspect.

// get the full authorization response returned by the gate
$response = Gate::inspect('edit');
 
if ($response->allowed()) {
  // Let the user edit the post
} else {
  // Display the denial message
  // 'You must be an administrator to edit posts.'
  echo $response->message();
}
"Laravel 6.0 is here! Check out what's new and what's changing!"

Tweet This

Laravel UI composer package

Another update to be aware of is the extraction of the laravel/ui package. Front-end scaffolding will still exist, it's just not included by default. This means in a new Laravel 6.0 application, you won't see any Vue or Bootstrap scaffolding.

The package now includes presets for React, Vue, and Bootstrap. In a tweet back in May, Otwell mentions that he's considering adding a Tailwind option as well!

I’ve created https://t.co/IPvq4PdlrV to experiment with extracting Laravel’s front-end scaffolding. Give it a look! Welcome any improvements and will probably explore a @tailwindcss option. 🔥 “artisan ui vue —auth” … “artisan ui react —auth”
— Taylor Otwell 🏝 (@taylorotwell) May 31, 2019

If you'd still like it included, you can bring it back in with composer require laravel/ui.

New branding

With every major release, there has also been new branding that comes along with it, and 6.0 is no exception!

In addition to the new logo above, you can also find a fresh new redesign of the Laravel.com website.

Laravel Vapor

And last but certainly not least, the release of Laravel 6.0 also includes the launch of a much-anticipated product in the Laravel ecosystem - Laravel Vapor.

Before this release, most people used Laravel Forge to provision and deploy their apps. With Forge, you could connect your chosen server (Digital Ocean, AWS, etc.) and Forge would provision it specifically for your Laravel application. This is awesome of course, but you still had to manage updates for yourself.

Laravel Vapor does all of this and more. Instead of managing and updating servers for your Laravel application on your own, Vapor is completely serverless!

This doesn't mean there are no servers involved, it just means you don't have to deal with them. Another benefit is that you only pay for what you use. Instead of paying a flat monthly rate, you're only charged when a request is made to your application. This also means you don't have to worry about scaling, as it will automatically be done for you.

Here are some more awesome features that Vapor provides:

  • On-demand scaling - Can execute jobs immediately as they come in
  • Powered by AWS
  • Ready for bursts of traffic
  • Zero downtime during deployment
  • Extremely fast
  • Multiple environments with free vanity URL for testing (https://snowy-hurricane-12349834324432.vapor.build)
  • Maintenance mode
  • Searchable logs
  • Create and scale your databases from Vapor
  • Backup and restore databases
  • Metrics and monitoring with notifications
  • Purchase domains and manage DNS from Vapor dashboard
  • Automatically added SSL certificates
  • Jobs, workers, PHP updates, and more are handled behind the scenes
  • Deployment easily configured with a simple vapor.yaml file

The cost for all of this is $39 per month or $399 per year with unlimited team members and projects. Keep in mind, you still have to pay for your own AWS costs beyond that.

Upgrading to 6.0

The Laravel documentation estimates it should take about one hour to upgrade from 5.8 to 6.0. Your project may have other dependencies and external packages that can increase that time, so keep that in mind when you decide to upgrade.

How to upgrade to Laravel 6.0

In your composer.json file, change the Laravel framework dependency from 5.8. to ^6.0. If you're on a version earlier than 5.8, it's recommended to upgrade up to 5.8 before making the switch to 6.0.

// In composer.json
"laravel/framework": "^6.0", 

Then in your terminal run:

composer update

Laravel Shift

Another awesome way to manage your upgrades, especially for bigger applications, is to use Laravel Shift. Shift is an automated tool that will do your upgrade for you.

It works like this:

  • Grant Shift access to your Laravel repository
  • Choose what version of Laravel you're on and what you want to upgrade to
  • Shift creates a new branch and modifies what needs to be changed in your code
  • Shift creates a pull request so that you can test and decide if it should be merged

The process is very simple and a great option if simply changing the package version in your composer.json didn't work for you. The service isn't free, but the one-time fee is very low.

You can find the upgrade instructions for 6.0 here.

Do I need to Upgrade?

Laravel 6.0 is the latest Long Term Support release, which means bug fixes will be guaranteed for two years and security fixes for three years. The previous LTS release with these guarantees was Laravel 5.5.

This table from the Laravel website outlines the current support schedule:

With bug fixes for the previous LTS release ending very soon, it may be worth it to upgrade to 6.0 so that you know your project will be supported for a couple more years.

Aside: Securing Laravel APIs with Auth0

Securing Laravel APIs with Auth0 is very easy and brings a lot of great features to the table. With Auth0, we only have to write a few lines of code to get:

Sign Up for Auth0

You'll need an Auth0 account to manage authentication. You can sign up for a free account here. Next, set up an Auth0 API.

Set Up an API

Go to APIs in your Auth0 dashboard and click on the "Create API" button. Enter a name for the API. Set the Identifier to a URL(existent or non-existent URL). The Signing Algorithm should be RS256.

Create API on Auth0 dashboard

We're now ready to implement Auth0 authentication on our Laravel backend API.

Dependencies and Setup

Install the laravel-auth0 package via composer like so:

composer require auth0/login:"~5.0"

Generate the laravel-auth0 package config file like so:

php artisan vendor:publish

After the file is generated, it will be located at config/laravel-auth0.php. Ensure you replace the placeholder values with the authentic values from the Auth0 Admin Dashboard. Double check your values with laravel-auth0.

Your .env file should have the AUTH0DOMAIN, AUTH0CLIENT_ID, AUTH0_CLIENT_SECRET and AUTH0_CALLBACK_URL values like so:

AUTH0_DOMAIN=kabiyesi.auth0.com
AUTH0_CLIENT_ID=xxxxxxxxxxxxxxxxxx
AUTH0_CLIENT_SECRET=xxxxxxxxxxxxxxxxx
AUTH0_AUDIENCE=http://mylaravelapi.com
AUTH0_CALLBACK_URL=null

Activate Provider and Facade

The laravel-auth0 package comes with a provder called LoginServiceProvider. Add this to the list of application providers.

// config/app.php
'providers' => array(
    // ...
    \Auth0\Login\LoginServiceProvider::class,
);

If you would like to use the Auth0 Facade, add it to the list of aliases.

// config/app.php
'aliases' => array(
    // ...
    'Auth0' => \Auth0\Login\Facade\Auth0::class,
);

The user information can now be accessed with Auth0::getUser(). Finally, you need to bind a class that provides a user (your app model user) each time the user is logged in or an access_token is decoded. You can use the Auth0UserRepository provided by this package or you can build your own class.

To use Auth0UserRepository, add the following lines to your app's AppServiceProvider:

// app/Providers/AppServiceProvider.php
public function register()
{
    $this->app->bind(
      \Auth0\Login\Contract\Auth0UserRepository::class,
      \Auth0\Login\Repository\Auth0UserRepository::class
    );
}

Configure Authentication Driver

The laravel-auth0 package comes with an authentication driver called auth0. This driver defines a user structure that wraps the normalized user profile defined by Auth0. It doesn't actually persist the object but rather simply stores it in the session for future calls.

This is adequate for basic testing or if you don't have a requirement to persist the user. At any point you can call Auth::check() to determine if there is a user logged in and Auth::user() to retreive the wrapper with the user information.

Configure the driver in config/auth.php to use auth0.

// app/config/auth.php
// ...
'providers' => [
    'users' => [
        'driver' => 'auth0'
    ],
],

Secure API Routes

Your API routes are defined in routes/api.php for Laravel 5.3+ apps.

// routes/api.php
 
<?php
 
use Illuminate\Http\Request;
 
/
|--------------------------------------------------------------------------

API Routes
Here is where you can register API routes for your application. These
routes are loaded by the RouteServiceProvider within a group which
is assigned the "api" middleware group. Enjoy building your API!

*/
 
Route::get('/public', function (Request $request) {
  return response()->json(["message" => "Hello from a public endpoint! You don't need any token to access this URL..Yaaaay!"]);
});
 
Route::get('/wakanda', function (Request $request) {
  return response()->json(["message" => "Access token is valid. Welcome to this private endpoint. You need elevated scopes to access Vibranium."]);
})->middleware('auth:api');

Now, you can send a request to your protected endpoint which includes an access_token.

curl --request GET 
  --url http://localhost:8000/api/wakanda
  --header 'authorization: Bearer <ACCESS TOKEN>'

Once a user hits the api/wakanda endpoint, a valid JWT access_token will be required before the resource can be released. With this in place, private routes can be secured.

Thanks for reading

If you liked this post, share it with all of your programming buddies!

Follow me on Facebook | Twitter

Further reading

PHP with Laravel for beginners - Become a Master in Laravel

Projects in Laravel: Learn Laravel Building 10 Projects

Laravel for RESTful: Build Your RESTful API with Laravel

Fullstack Web Development With Laravel and Vue.js

Laravel 5.8 Tutorial for Beginners

Laravel 5.8 Ajax CRUD tutorial using Datatable JS

Laravel 5.8 Tutorial from Scratch for Beginners

Build RESTful API In Laravel 5.8 Example

Login with Google in Laravel 5.8 App using Socialite Package

Upgrading Laravel To 6.0 From 5.8

Laravel 6 Release New Features and Upgrade

Advantages of Hiring PHP Developer for your Website Project

Advantages of Hiring PHP Developer for your Website Project

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is...

PHP - Hypertext pre-processor, a scripting language used by many people in developing web pages, but most of us are unaware even of the full form. To train someone and make them learn this whole language is as difficult and time-consuming as it is to learn the language yourself. That’s why PHP developers are there to make your life easy. This article will give us the advantages and requirements of Hire PHP Developer for our very own website project.

First of all, let us understand the value the right developer brings to the project and why it is important for your business.

A website is a major component of any company/business and is very important for its face value, the way it represents the company on the internet is critical for any business to succeed. This is the reason why companies are looking for PHP developers who can develop their webpage.

If you're planning to do an online business, your PHP programmer will be the first person to transfer your thinking onto the webpage. You should, therefore, employ developers from PHP to make your hypothetical idea a reality.

With this software programming language, PHP developers all-around can easily build website frameworks, web content management systems, web template systems, and various other web-based designs.

Some of the reasons why we need to outsource these developers are:

Not everyone is the best in each field, all of us have our specific skills and talents hence, PHP developers are also the best at what they do. The time and money spent on the training of the in house employees would be saved if the professional PHP developers are hired. Instead of multitasking, if the employees were to focus on what they’re good at it would increase productivity too.

The PHP developers would be much more professional than the in-house workers. It would lead to the seriousness of work. Hence, on-time delivery is guaranteed with hired PHP developers.

In addition to these benefits, you would also be able to track your project through every stage in constant communication with your online team. These advantages make it incredibly popular and smart to hire a PHP developer.

The PHP developers have in-depth knowledge of PHP, HTML and various frameworks in terms of technical capabilities. Hiring PHP developers are advised to give your website a professional look based on PHP.

Much of web success depends on the involvement of social media. The developer can add to your social networking pages a feature that explicitly redirects visitors. In addition, SEO experts also suggest better connections to the website's social network.

Just like a tailor stitches our dresses according to our preferences and is ready to make last-minute changes. A PHP developer will also be available at the nick of your call to make the website just the way you want it to be and have a customized solution for every problem.

Read also: Why & How to Hire Dedicated PHP Developer

At some point in your business, you’re going to have problems regarding your webpage due to the rapidly changing technology, instead of struggling with ideas like these and not being able to come up with an appropriate solution a PHP web developer could help us with our problems just like any technician would help us with the problems we face in our offices or any architect would help us with designing the structure of a building or any interior designer would help us with setting up our home. The PHP development company are hubs of workers who would help us overcome these problems and are always there.

Source by

Hire PHP Developer and Web Developer for your Online Business

Hire PHP Developer and Web Developer for your Online Business

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application...

PHP is widely used open-source scripting language it helps in making dynamically easy your websites and web application. Mobiweb Technology is your best technical partner and offering you solution for any kind of website and application development. To hire PHP developer and web developer at affordable prices contact Mobiweb Technology via [email protected]

Laravel Framework Development Services | Laravel Web Development

CMARIX is leading Laravel Web application Development Service Provider with the track record of implementing diverse high-performance Laravel web applications with skilled Laravel developer team for clients across the globe<a href="https://www.cmarix.com/laravel-framework-development-services.html?utm_source=SB" target="_blank">.Continue</a>

CMARIX is leading Laravel Web application Development Service Provider with the track record of implementing diverse high-performance Laravel web applications with skilled Laravel developer team for clients across the globe.Continue