0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

phpとlaravelバージョンアップデート(2024版)

Last updated at Posted at 2025-02-09

事前準備

OS/ミドルウェア 現バージョン  新バージョン
PHP 7.x系 8.x系
Laravel 5.x系 11.x系

更新作業

1. Exception更新

Laravel 7からExceptionはThrowableに更新するので、更新してください。

  • app/Exceptions/Handler.php更新
-use Exception;
+use Throwable;

-    public function report(Exception $e)
+    public function report(Throwable $e)

-    public function render($request, Exception $exception)
+    public function render($request, Throwable $exception)
  • 他のファイルにてExceptionが利用される場合、更新

2. モーダルでprotected $dates更新

- protected $dates = ['created_at', 'updated_at'];
+ protected $casts = [
+     'created_at' => 'datetime',
+     'updated_at' => 'datetime',
+ ];
それとも
+ protected function casts(): array
+ {
+     return [
+     'created_at' => 'datetime',
+     'updated_at' => 'datetime',
+     ];
+ }

3. proxy更新

fideloper/proxyがストップされたので、laravelのproxyを利用してください。

source/app/Http/Middleware/TrustProxies.php

-use Fideloper\Proxy\TrustProxies as Middleware;
+use \Illuminate\Http\Middleware\TrustProxies as Middleware;

-    protected $headers = Request::HEADER_X_FORWARDED_ALL;
+    protected $headers =
+        Request::HEADER_X_FORWARDED_FOR |
+        Request::HEADER_X_FORWARDED_HOST |
+        Request::HEADER_X_FORWARDED_PORT |
+        Request::HEADER_X_FORWARDED_PROTO |
+        Request::HEADER_X_FORWARDED_AWS_ELB;

4. composer更新

  • 旧のcomposer.jsonとcomposer.lockを削除して、laravel11のcomposer.jsonを作成する
  • 旧のcomposer.jsonの必要なパッケージを新composer.jsonに以下のコマンドで追加する
composer require パッケージ名
composer require --dev パッケージ名

修正完了してからインストール

composer install

5. 古いヘルパー対応

array_excel、str_pluralなどヘルパー関数が削除され他ので、

// 前
array_except($array, ['key']);
str_plural('car');

// 後
Arr::except($array, ['key']);
Str::plural('car');
  • 案2:laravel/helpersパッケージをインストールする。旧のヘルパー関数が利用される
composer require laravel/helpers

6. redirect()

Route::resource('user', 'UserController');

- return redirect(route('user.edit', ['id' => $user['id']]));
+ return redirect(route('user.edit', ['user' => $user['id']]));

7. validationData()

- protected function validationData()
+ public function validationData()

その他

アップグレードガイドを参考ながら、修正しえてください。
https://laravel.com/docs/6.x/upgrade

ビュー

1. コンポーネント更新

// 前
@component('components.alert')
    Alert Body
@endcomponent

// 後
<x-alert>
    Alert Body
</x-alert>

2. ビューのlaravelcollective/htmlを更新

全てビューファイルでの以下のような「Form::」のコードが利用できないです。

{!! Form::text('name', 'default value', ['class' => 'form-control']) !!}

以下の案に修正してください。案2がおすすめです
案1:htmlソースに変わる

<input type="text" name="name" value="default value" class="form-control">

案2:spatie/laravel-htmlを利用して、「Form::」のソースを修正

{!! html()->text('name')->value('default value')->class('form-control') !!}

ビュー更新ツール

ビューの修正はかなり大変になるので、ビュー交換ツール作成して、別の表記で登録させていただきます。

以上です。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?