13

NB: Local server PHP Version 8.1.4, laravel project inside composer.json file have "php": "^7.2.5", version & "laravel/framework": "^7.0"

PHP Fatal error: During inheritance of ArrayAccess: Uncaught ErrorException: Return type of Illuminate\Support\Collection::offsetExists($key) should either be compatible with ArrayAccess::offsetExists(mixed $offset): bool, or the #[\ReturnTypeWillChange] attribute should be used to temporarily suppress the notice

error displaying below like this :

Alal Sardar
  • 139
  • 1
  • 1
  • 4
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jun 07 '22 at 11:42
  • Either lower your PHP version level because Laravel framework version 7 does not support it, or upgrade your Laravel version to 9 or 10 to make it compatible with PHP 8.1 – Joel Harkes Mar 31 '23 at 09:17

6 Answers6

8

That's a php version issue. Upgrade laravel or downgrade PHP. This article covers how to downgrade the PHP version so it matches the Laravel Version.

https://bytexd.com/fix-laravel-return-type-of-illuminatesupportcollectionoffsetexistskey/

George Geoker
  • 342
  • 1
  • 3
  • 13
6

in your composer.json update line

"php": "^7.3",

to

"php": "^7.3|^8.1",

and run composer update

nasim jahednia
  • 165
  • 1
  • 8
3

You would need to upgrade your Laravel Framework version to at least version 8 and it's dependencies using composer.

Reference: https://laravel.com/docs/8.x/releases

Or if you want to postpone it, you would need to add #[\ReturnTypeWillChange] before declaring every function that throws the error. This is highly not advisable.

P.S. If you updated your PHP version you would have to change the PHP version in composer to the one used, in your case it should be "php": "^8.1.4"

Velikov
  • 31
  • 3
2

In my case, using a mac, turns out my php version was 8.1 which somehow is not compatible with laravel version 7. Tho most of these answers are correct, but they did not fix my issues. I followed these steps (if brew is installed):

  1. brew unlink php@8.1
  2. brew link php@8.0 or lower

Reference How can I easily switch between PHP versions on Mac OSX?

Buchi
  • 368
  • 1
  • 4
  • 16
0

This Issue Occurred because laravel 7 is not supported with PHP 8.1, so mean While You need to downgrade your php version to <=8.0. This worked for me.

Hope this will help you too.

-2

run "composer update" on your project directory and it will work .

  • 3
    Hello Sunny, welcome to Stackoverflow. Please ensure that you read the question fully and are certain of the answer. In this case, running a composer update - although it may solve the problem - triggers a full-on Laravel upgrade which is far more costly than just switching PHP versions on the local machine. I've down-voted your answer but I think you could edit it to explain what the problem is and what composer update will do. – Mike Elahi Nov 11 '22 at 11:32