Sep
17
2019

If you want to play with the upcoming new version of Laravel (6.0) and starting your laravel development with laravel 6.0 then this article is for you.

– How to Install Laravel 6.0
Laravel utilizes Composer to manage its dependencies. So, before using Laravel,
make sure you have Composer installed on your machine.

Via Laravel Installer

First, download the Laravel installer using Composer
composer global require laravel/installer
Make sure to place Composer’s system-wide vendor bin directory in your $PATH so the laravel executable can be located by your system. This directory exists in different locations based on your operating system; however, some common locations include.


macOS and GNU / Linux Distributions: $HOME/.composer/vendor/bin
Windows: %USERPROFILE%\AppData\Roaming\Composer\vendor\bin

After that you can simply create laravel new project by running this command laravel new blog

Via Composer Create-Project

Alternatively, you may also install Laravel by issuing the Composer create-project command in your terminal.


composer create-project --prefer-dist laravel/laravel blog

– Laravel New Feature

– Improved Authorization Responses
In previous version of Laravel it’s difficult to show custom authorization messages to end users. But in laravel 6.0 Gate::inspect method to show custom message.
Example:-

Authorization in previous version of laravel
Create Policy


/**
 * Determine if the given post can be updated by the user.
 *
 * @param  \App\User  $user
 * @param  \App\Post  $post
 * @return bool
 */
public function update(User $user, Post $post)
{
  return $user->id === $post->user_id;
}

Register policy in AuthServiceProvider


protected $policies = [
   'App\Model' => 'App\Policies\ModelPolicy',
    Post::class => PostPolicy::class
];

Authorize user


public function update(Request $request, Post $post)
{
    $this->authorize('update', $post);

    // The current user can update the blog post...
}

Authorization in laravel 6.0
Create Policy


/**
 * Determine if the given post can be updated by the user.
 *
 * @param  \App\User  $user
 * @param  \App\Post  $post
 * @return bool
 */
public function update(User $user, Post $post)
{
if($user->id !== $post->user_id){
  $this->deny(‘User can not update post ’);
}	
  return $user->id === $post->user_id;
}

Authorize User


public function update(Request $request, Post $post)
{
$response = Gate::inspect('update', $post);

if ($response->allowed()) {
    // User is authorized to update post
}

if ($response->denied()) {
    echo $response->message();
}
}

– Lazy Collections

Laravel 6.0, the query builder’s cursor method has been updated to return a LazyCollection instance.The cursor method allows you to iterate through your database records using a cursor, which will only execute a single query. When processing large amounts of data, the cursor method may be used to greatly reduce your memory usage.
Example Usage :-


foreach (Product::where('name', 'bar')->cursor() as $flight) {
 //do some stuff
}
cursor(): High Speed
chunk(): Constant Memory Usage
10,000 records:

+---------------- +-----------------+------------------+
|      		| Time(sec) 	| Memory(MB) |
+-------------+-----------+----------------------------+
| get()		|      0.17 	|         22 	 |
| chunk(100)  	|      0.38	|         10 	 |
| chunk(1000)	|      0.17 	|         12  	 |
| cursor()    	|      0.16 	|         14 	 |
+----------------+------------------+------------------+

TestData: users table of Laravel default migration

– Laravel/ui

The Bootstrap and Vue scaffolding provided by Laravel is located in the laravel/ui Composer package, which may be installed using Composer.

-Composer require laravel/ui

Once the laravel/ui package has been installed, you may install the frontend scaffolding using the ui Artisan command.


// Generate basic scaffolding...
php artisan ui vue
php artisan ui react

// Generate login / registration scaffolding...
php artisan ui vue --auth
php artisan ui react --auth

– Eloquent Subquery Enhancements

Laravel 6.0 introduces several new enhancements and improvements to database subquery support. For example, let’s imagine that we have a table of flight destinations and a table of flights to destinations. The flights table contains an arrived_at column which indicates when the flight arrived at the destination.Using the new subquery select functionality in Laravel 6.0, we can select all of the destinations and the name of the flight that most recently arrived at that destination using a single query.
Example :-


return Destination::addSelect(['last_flight' => Flight::select('name')
    ->whereColumn('destination_id', 'destinations.id')
    ->orderBy('arrived_at', 'desc')
    ->limit(1)
])->get();