Laravel 5.5 New Release Features

Laravel 5.5 new release features are available now

Laravel 5.5 LTS is the next long term support version of Laravel. It’s recently released on August 30, 2017 and this is the current stable version of Laravel framework. We can download it directly from Laravel 5.5. we can also install through a repository. Just take clone of repository https://github.com/laravel/framework.git

New release version having many new features. This PHP application framework providing fast and very simple routing, powerful dependency injection container, real time event broadcasting and multiple backends for cache and session storage.

Laravel 5.5 new release having more than 10 new features.

1. Migration:fresh Artisan Migrate Command

– migration:fresh drop all tables and run again migration. It’s will run line first migration running.
– migration:refresh is rollbacking the migration. So, for development phase migration:fresh helping too much


$ php artisan migration:fresh

2. The withoutExceptionHandling() method

– When we don’t want any exception to catch or not worthy exception to catch. In some cases, we have to ignore exception or we are in development phase or doing testing that time withoutExceptionHandling() method helps a lot.

3. Automatic Package Discovery

4. Separate Factory Files

– Now we can create separate factory files for each model. Previously we have only one file ModelFactory.php.
– Factory file will create when we are creating a new model from artisan command.


//Create model with factory
$ php artisan make:model -fm Model

//Create individual factory for specific model
$ php artisan make:factory --model=Model ExampleFactory

5. Auth and Guest Blade Directives

– We are using auth()->check() or auth()->guest() to check authentication user. But now you can directly check authentication user using @auth and @endauth, @guest and @endguest().


@auth
    Hello {{ user()->name }}!
@endauth

@guest
    Hello {{ user()->name }}!
@endguest

6. Frontend Presets

– Now Laravel 5.5 provides Vue.js scaffolding by default. We can also remove front end scaffolding or change it to “bootstrap”, “react” or “none”.


// Set react as scaffolding
$ php artisan preset react

//Set none for scaffolding
$ php artisan preset none

7. Exception Rendering

– If Exception is defining a public “response” method then it can render now.
– In previously we are checking App\Exceptions\Handler::render() and conditionally send back a response based on the exception type.
– But in Laravel 5.5 we just have to throw the exception. It will automatically respond in your handler.
– You can also implement a responsable interface in your exception classes.


namespace App\Exceptions;

use App\Student;

class StudentException extends \Exception
{

    protected $student;

    public function __construct(Student $student)
    {
        $this->student = $student;
    }

    public function render($request)
    {
        return response("The student '{$this->student->name}' and '{$this->student->enrollment}' selection.");
    }

}

8. The Responsable Interface

– Responsable Interface is an another response addition to Laravel. From the controller method implementing interface can be returned to the route and route can check instance of responsable when preparing routing response. Basically, that’s used in case of AJAX request. Each time you want to redirect to a specific page after AJAX request OR JSON format.


public function store(Request $request)
{

    $data = request()->validate([
        'name' => 'required',
        'enrollment' => 'required|numeric',
        'joining_date' => 'required|date_format:Y-m-d',
        'result' => 'boolean',
    ]);

    $student = new Student($data);
    $student->save();

    return new NewStudentResponse($student);

}

And that response file will be.

namespace App\Http\Responses;

use App\Student;
use Illuminate\Contracts\Support\Responsable;

class NewStudentResponse implements Responsable
{
    protected $student;

    public function __construct(Student $student)
    {
        $this->student = $student;
    }

    public function toResponse($request)
    {
        if ($request->wantsJson()) {
            return response()
                ->json($this->student)
                ->header('Location', route('student.show', $this->student))
                ->setStatusCode(201);
        }
        return redirect()
            ->route('student.show', $this->student);

    }

}

9. Request Validation Method

– In past versions, we are using request instance to the $this->validate() method into the controller but as from now you can just call validate on the request object. Like request()->validate().
– This is act like Request::only()


    $data = request()->validate([
            'name' => 'required',
            'enrollment' => 'required|numeric',
            'joining_date' => 'required|date_format:Y-m-d',
            'result' => 'boolean',
        ]);

10. Custom Validation Rule Objects and Closures

– As from now, we can create our custom validation rule object. Before we are creating custom rules with Validator::extend. Creating custom validation rule from command
– $ php artisan make:rule MyCustomRule


request()->validate([
    'cowbells' => [new CowbellValidationRule],
    'more_cowbells' => [function ($attribute, $value, $fail) {
        if ($value <= 10) {
            $fail(':attribute needs more cowbell!');
        }
    }]
]);

11. Whoops Package Returns to Laravel 5.5

– filp/whoops is an error handler framework for PHP with pretty error interface.
– more info https://packagist.org/packages/filp/whoops

12. Collection Dumping

– Collection dumping method is great debugging feature
– When we are using a collection with filters and we have to stop the process at certain points. If using dump() within collections it will continue the process until the end. But dd() use on other hands to stops the process immediately and dumps out the results.


Song::all()
    ->filter
    ->platinum
    ->dump()
    ->filter(function ($song) {
        return $song->released_on >= \Carbon\Carbon::parse('-10 years');
    })
    ->dd();

13. The RefreshDatabase Trait

 

Many more features will be coming soon. Just take a glance on above 😉

Leave a Reply

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