Table of Contents
Error Message
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 in /var/www/html/vpn/vendor/laravel/framework/src/Illuminate/Support/Collection.php:1277\nStack trace:\n#0
Cause
The issue is caused by the incompatible versions of PHP and Laravel. Laravel 7 and lower don’t support PHP 8.1 (which comes with Ubuntu 22.04).
Fix
There are two ways to address this issue. You can downgrade the PHP version to a version supported by your Laravel version, or you can upgrade Laravel to a version that is supported by the PHP version.
Change the PHP version to a version supported by your current version of Laravel.
Upgrading Laravel can be complicated, depending on your project and how big the leap is. We’re going to cover the first method of fixing the issue.
Find out what version of Laravel your project is.
php artisan --version
In my case, the output is Laravel Framework 6.20.44.
By checking the table above, we see that we need PHP 7.2 – 8. We might also want to check the PHP version in the composer.json file from our project.
composer.json { "name": "laravel/laravel", "type": "project", "description": "The Laravel Framework.", "keywords": [ "framework", "laravel" ], "license": "MIT", "require": { "php": "^7.2", "david-griffiths/nova-dark-theme": "^1.0", "fideloper/proxy": "^4.0", "gregoriohc/laravel-nova-theme-responsive": "^0.6.2", "laravel/framework": "^6.0", "laravel/nova": "*", "laravel/tinker": "^1.0", "stephenlake/nova-fixed-bars": "^1.1", "titasgailius/search-relations": "^1.0" }, }
Check what version of PHP you’re running.
php -v PHP 8.1 +ubuntu22.04.1+deb.sury.org+1 (cli) (built: Aug 1 2022 08:55:49) ( NTS )
Because we’re running a version of PHP that is not supported by our project, we need to downgrade it to PHP 7.2.
Install PHP 7.2 on Ubuntu 22.04
We need to set up the ppa:ondrej/php
repository
sudo apt install software-properties-common ca-certificates lsb-release apt-transport-https
LC_ALL=C.UTF-8 add-apt-repository ppa:ondrej/php
sudo apt update
Install the PHP version you need and set it as default.
apt install php7.2 #you might want to install some common extentions as well apt-get install php7.2 php7.2-cli libapache2-mod-php7.2 php7.2-common php7.2-curl php7.2-mbstring php7.2-xmlrpc php7.2-mysql php7.2-gd php7.2-xml php7.2-intl php7.2-ldap php7.2-imagick php7.2-json
Now, let’s set it as our main version in the command line.
update-alternatives --config php
Check the PHP version again.
Configure Apache to use the newly installed PHP 7.2 version
First, we need to disable the old PHP 8.1 from Apache
sudo a2dismod php8.1
Now, enable PHP 7.2
sudo a2enmod php7.2
Restart Apache to apply the PHP version change.
service apache2 restart
when i check the version on command line it shows 8.1 but when I run php_info it shows 7.2
Hi,
Perhaps the php_info is ran using Apache. Please note that apache cand have a different PHP version set. I mentioned in the tutorial how you can change the apache php version.
pls which terminal should i use in running this
Create a test.php file
<?php
// Show all information, defaults to INFO_ALL
phpinfo();
?>
then run in terminal “php test.php”
also, try copy the test.php file in the Apache files /var/www/html
Then try using a browser to access it and see what version you’re getting
This works for me. Thanks.