Upgrade Guide


 laravel_upgrade_5.5_5.4

Note: The estimated time for upgrading from Larave 5.4 to Larave 5.5 is about 1 hour.

Update Dependencies

to update composer.json In file laravel/framework Dependent version is 5.5.* In addition, you need to update phpunit/phpunit Dependent version to ~6.0
Note: If you usually pass laravel new To use the Larravel installer, you also need to use the composer global update Command to update the Laravel installer.
Laravel Dusk

Laravel Dusk 2.0.0 Has been released to be compatible with Larravel 5.5.

Pusher

Pusher event broadcast driver now needs ~3.0 Version of the Pusher SDK.

Artisan

fire method

All in Artisan command fire Need to be renamed to handle

optimize command

With the recent optimization of PHP opcode cache, optimize The command is no longer needed. You should remove the reference to the command from the deployment script, because the command will not be available in future versions.

to grant authorization

authorizeResource Controller method

When a model name containing multiple words is passed to authorizeResource Method, the resulting route fragment will be in a dash style (snake) to match the behavior of the resource controller. For example, the model name is UserPost , the corresponding result route segment is user_post

before Strategic approach

If the policy class does not contain a method name that matches the check permission name, the policy class's before The method will not be called.

cache

Database driven

If your application uses the database cache driver, you need to run it when you first deploy the upgraded Larravel 5.5 application php artisan cache:clear

Eloquent

belongsToMany method

If you rewrite the Eloquent model belongsToMany Method, you need to modify the method signature to reflect the new parameter:

 /** * Define a many-to-many relationship. * * @param  string  $related * @param  string  $table * @param   string  $foreignPivotKey * @param   string  $relatedPivotKey * @param  string  $parentKey * @param  string  $relatedKey * @param  string  $relation * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany */ public function belongsToMany($related, $table = null, $foreignPivotKey = null, $relatedPivotKey = null, $parentKey = null, $relatedKey = null, $relation = null) { // }

Model is method

If you rewrite the Eloquent model is Method, which needs to be removed Model Type prompt, because this method can receive null As a parameter:

 /** * Determine if two models have the same ID and belong to the same table. * * @param  \Illuminate\Database\Eloquent\Model|null  $model * @return bool */ public function is($model) { // }

Model $events attribute

In the model $events Property needs to be renamed to $dispatchesEvents The reason for this change is that many users need to define $events This will conflict with the original attribute name.

Pivot $parent attribute

Illuminate\Database\Eloquent\Relations\Pivot Protected Properties in $parent Was renamed to $pivotParent

Associated create method

BelongsToMany HasOneOrMany and MorphOneOrMany Of create Methods have been adjusted to $attributes The parameter provides a default value. If you override these methods, you need to update the method signature to match the new definition:

 public function create(array $attributes = []) { // }

Soft deleted model

When deleting a "soft deleted" model exists The attribute will return true

withCount Field format

When using aliases, withCount Method is no longer automatically appended _count To the result field name, for example, in Larravel 5.4, the following query will add a bar_count Field to query result:

 $users = User::withCount('foo as bar')->get();

However, in Larravel 5.5, this alias will directly use the given alias, if you want to add _append To the result field, you must specify when defining the alias:

 $users = User::withCount('foo as bar_count')->get();

Exception format

In Laravel 5.5, all exceptions, including validation exceptions, will be converted into HTTP responses through the exception handler. In addition, the default JSON format for validation errors has also been adjusted. The new format follows the following conventions:
 { "message": "The given data was invalid.", "errors": { "field-1": [ "Error 1", "Error 2" ], "field-2": [ "Error 1", "Error 2" ], } }
However, if you want to maintain the JSON error format in Larravel 5.4, you can add the following methods to App\Exceptions\Handler Class:
 use Illuminate\Validation\ValidationException;

/**

  • Convert a validation exception into a JSON response.
  • @param \Illuminate\Http\Request $request
  • @param \Illuminate\Validation\ValidationException $exception
  • @return \Illuminate\Http\JsonResponse */ protected function invalidJson($request, ValidationException $exception) { return response()->json($exception->errors(), $exception->status); }
JSON login attempt

This change will also affect the format of validation errors. For the JSON format returned by login attempts, in Laravel 5.5, the JSON error message returned by login failures follows the new format convention described above.

Notes on form request

If you have customized the response format for an independent form request, you need to rewrite the failedValidation Method, and throw the HttpResponseException example:

 use Illuminate\Http\Exceptions\HttpResponseException; /** * Handle a failed validation attempt. * * @param  \Illuminate\Contracts\Validation\Validator  $validator * @return void * * @throws \Illuminate\Validation\ValidationException */ protected function failedValidation(Validator $validator) { throw new HttpResponseException(response()->json(..., 422)); }

file system

files method

Illuminate\Filesystem\Filesystem Class files Method changes its method signature to a new one $hidden Parameter and return SplFileInfo Object arrays, and allFiles The method is similar. In previous versions, files Method returns an array of string path names. The new method signature is as follows:

 public function files($directory, $hidden = false)

mail

Unused parameters

Unused $data and $callback Parameter has been changed from Illuminate\Contracts\Mail\MailQueue Contractual queue and later Method:

 /** * Queue a new e-mail message for sending. * * @param   string|array|MailableContract  $view * @param  string  $queue * @return mixed */ public function queue($view, $queue = null); /** * Queue a new e-mail message for sending after (n) seconds. * * @param  \DateTimeInterface|\DateInterval|int  $delay * @param   string|array|MailableContract  $view * @param  string  $queue * @return mixed */ public function later($delay, $view, $queue = null);

request

all method

If you rewrite Illuminate\Http\Request Class all Method, the method signature needs to be modified to reflect the newly added $keys Parameters:

 /** * Get all of the input and files for the request. * * @param  array|mixed  $keys * @return array */ public function all($keys = null) { // }

has method

$request->has The method now returns true , even if the input value is an empty string or null , a new $request->filled Method was added to replace the previous has Method.

intersect method

intersect The method has been removed. You can use the $request->only Called on array_filter Method to complete the same function:

 return array_filter($request->only('foo'));

only method

only The method now only returns the properties that appear in the request payload. If you want to maintain the previous only Method function, you can use all Method instead of:

 return $request->all('foo');

request() auxiliary function

request The auxiliary function no longer receives nested keys. If necessary, you can use the input Method to complete this function:

 return request()->input('filters.date');

test

Authentication Assertion

Some authentication assertions have been renamed to maintain better consistency with other assertions in the framework:

  • seeIsAuthenticated Was renamed to assertAuthenticated
  • dontSeeIsAuthenticated Was renamed to assertGuest
  • seeIsAuthenticatedAs Was renamed to assertAuthenticatedAs
  • seeCredentials Was renamed to assertCredentials
  • dontSeeCredentials Was renamed to assertInvalidCredentials
Mail forgery

If you are using Mail Forgery is used to determine whether a mail object is pushed to the queue in the request phase. Now you need to use Mail::assertQueued To replace Mail::assertSent The difference between this distinction is that you can clearly assert that the message is pushed to the queue and sent through the background task instead of being sent during the request process.

translate

LoaderInterface

Illuminate\Translation\LoaderInterface Interface moved to Illuminate\Contracts\Translation\Loader Next.

verification

Validator methods

All validator methods now have modifiers public instead of protected

view

Dynamic variable name

When we allow dynamic __call When methods share variables through views, these variables automatically use the hump style, such as:

 return view('pool')->withMaximumVotes(100);

maximumVotes Variables can be accessed in the template in the following ways:

 {{ $maximumVotes }}

other

We also encourage you to laravel/laravel Github warehouse You may want to keep these files synchronized in your application, although many changes are unnecessary. Some files will be overwritten in this upgrade, but other files, such as configuration files or annotation adjustments, will not. You can easily Github comparison tool Review these changes and select the updates that are important to you.

give the thumbs-up Cancel Like Collection Cancel Collection

<<Previous: New Features

>>Next: Installation Configuration